# How do I check memory and CPU usage on a Linux server?

Check memory and CPU usage on a Linux server: read free -h and available, find the biggest process with ps --sort, and use top, vmstat and the load average.

Source: https://valakes.com/learn/memory-and-cpu-usage/

Last updated: 2026-11-25

Run free -h and read the available column. It is the memory that new programs can still get without swapping. Then run ps -eo pid,user,rss,comm --sort=-rss to see which process holds the most, and sort by -pcpu to see which one burns the CPU.

Commands checked on Ubuntu 24.04.5 LTS (the ubuntu:24.04@sha256:562111b88caa79c71391c0c4d3c2114a7d5c35915c20561fd893cb1e07929e6b container image), 25 November 2026.

Run `free -h` and read the `available` column, then list the processes by memory with `ps`. If the machine feels slow and memory is fine, sort the same list by CPU and compare the load average with the number of CPUs. Four tools do the job: `free`, `ps`, `top` and `vmstat`.

The server below runs two toy programs as the user `dev`. `report-builder` keeps about 200 MB in memory, and `log-compactor` uses one CPU all the time. They stand in for the two kinds of trouble you look for.

## How much memory is left

`free -h` prints one table for memory and one for swap, in units that are easy to read. The header shows the columns:

```console
$ free -h | head -1
               total        used        free      shared  buff/cache   available
```

The numbers under it change every second, so this article does not print them. Run the command on your server and read it like this:

- `total` is the memory the machine has.
- `free` is memory that nothing uses at all. On a healthy server it is small, because Linux fills spare memory with the cache of files it has read.
- `buff/cache` is that cache. The kernel gives most of it back when a program asks for memory.
- `available` is the number that answers the question. It is the kernel's estimate of how much memory new programs can get without swapping.

Swap in use is not a problem by itself. A machine that keeps moving pages in and out of swap is, and `vmstat` below shows that.

In a container these commands show the memory and the CPUs of the whole host, not the limit of the container. `nproc` prints the number of CPUs the machine reports. The number differs from server to server, so this article does not print it.

## Which process holds the memory

`ps` lists processes. `-e` takes all of them, `-o` picks the columns, and `--sort=-rss` orders them by resident memory, biggest first. The minus sign means descending. `rss` is in kilobytes. Run it with `| head` and you see the biggest processes. Here `awk` keeps the first row and rounds the size to megabytes, because the exact kilobytes move a little from run to run:

```console
$ ps -eo pid,user,rss,comm --sort=-rss | awk 'NR==2 {printf "%s about %d MB %s\n", $2, int($3/10240+0.5)*10, $4}'
dev about 200 MB report-builder
```

`report-builder` holds about 200 MB. The same list sorted by CPU:

```console
$ ps -eo user,comm --sort=-pcpu | head -2
USER     COMMAND
dev      log-compactor
```

A warning from the `ps` manual: `pcpu` is the CPU time the process has used divided by the time it has been running. A process that was busy for an hour and is idle now keeps a high value for a long time. For what happens right now, use `top`.

## top, once

`top` is a live screen, which is not what you want in a script or a log. `-b` is batch mode, `-n 1` takes one snapshot and `-o %MEM` sorts by memory. `-w 200` makes the lines wide enough that the command name is not cut. The first command prints the header row. The second prints the user and the command of the first process, since the numbers in that row, the PID among them, change from run to run:

```console
$ top -b -n 1 -w 200 -o %MEM | sed -n 7p
    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
$ top -b -n 1 -w 200 -o %MEM | awk 'NR==8 {print $2, $12}'
dev report-builder
```

In the interactive `top`, `M` sorts by memory, `P` by CPU, `1` shows each CPU on its own line and `q` quits. The `%CPU` of one process is relative to one CPU, so a program with several threads can show more than 100.

## Is the machine swapping, and is it waiting for the disk

`vmstat 1 2` prints a line now and another after one second. The first line is the average since the last boot, so read the second:

```console
$ vmstat 1 2 | head -2
procs -----------memory---------- ---swap-- -----io---- -system-- -------cpu-------
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st gu
$ vmstat 1 2 | tail -1 | awk '{print "b=" $2, "si=" $7, "so=" $8}'
b=0 si=0 so=0
```

`r` is the number of processes that want a CPU, and `b` is the number that sleep until input or output finishes. `si` and `so` are the memory moved in from swap and out to it each second. `wa` is the share of time the CPUs wait for the disk. A machine whose `si` and `so` stay well above zero for minutes is short of memory. A high `wa` points at a slow or busy disk, and [the disk article](https://valakes.com/learn/check-disk-space/) is the next stop.

## Load average

The command `uptime` ends with three numbers, the load averages of the last 1, 5 and 15 minutes. Each is the average number of processes that are running or waiting to run, plus those that wait for input or output. Divide by the number of CPUs from `nproc`. A load of 8 on a machine with 8 CPUs means that every CPU is busy all the time. Compare the three numbers with each other: a first number above the third says that the load is rising.

## Mistakes people make

You worry about a low `free`. Look at `available`.

You read a `%CPU` of 100 as a full machine. It is one full CPU. Count the CPUs first.

You kill the biggest process before you know what it is. Find out which program it belongs to and who started it. If it listens on a port, [which process uses a port](https://valakes.com/learn/process-on-port/) shows how to link the two.

You forget to check whether the kernel already killed something. When memory runs out, the kernel stops a process and writes a line such as `Out of memory: Killed process 1234 (name)` to the kernel log. A service with a memory limit gets `Memory cgroup out of memory` instead. Read the log with `sudo journalctl -k -g 'Killed process'` or `sudo dmesg | grep -i 'killed process'`. [Reading server logs](https://valakes.com/learn/read-server-logs/) covers the other logs.

## In Valakes

To watch top on several servers at once, open them as panes of one tab and start a broadcast in Keystrokes mode. Every key you type goes to every pane. Valakes runs no command check on keystrokes, so use it for a program that only reads, such as top. [Feature: Panes and broadcast](https://valakes.com/features/panes-and-broadcast/)

[Read the docs: Panes and broadcast](https://docs.valakes.com/panes-and-broadcast/)

## Questions

### What does "available" mean in the output of free?

It is the kernel's estimate of how much memory can go to new programs without swapping. It counts the free memory and the part of the cache that the kernel can give back.

### Is it a problem that free shows almost no free memory?

No. Linux uses spare memory for the cache of files and returns it when a program asks. Look at available, not at free.

### What is a high load average?

Compare the three numbers from uptime with the number of CPUs that nproc prints. A load near that number means every CPU is busy. A load that stays well above it means work is waiting for a CPU or for the disk.

## References

- [free(1) on Ubuntu 24.04, the columns of the memory table](https://manpages.ubuntu.com/manpages/noble/man1/free.1.html)
- [ps(1) on Ubuntu 24.04, --sort and the rss and pcpu fields](https://manpages.ubuntu.com/manpages/noble/man1/ps.1.html)
- [top(1) on Ubuntu 24.04, batch mode](https://manpages.ubuntu.com/manpages/noble/man1/top.1.html)
- [vmstat(8) on Ubuntu 24.04](https://manpages.ubuntu.com/manpages/noble/man8/vmstat.8.html)
- [uptime(1) on Ubuntu 24.04, the load averages](https://manpages.ubuntu.com/manpages/noble/man1/uptime.1.html)
- [The /proc file system, kernel documentation (MemAvailable in /proc/meminfo)](https://www.kernel.org/doc/html/latest/filesystems/proc.html)

## Related articles

- [How do I check disk space on a Linux server?](https://valakes.com/learn/check-disk-space/): Run df -h to see how full each file system is, then run du -h --max-depth=1 on the full one to find the folder that uses the space. df reads the file system itself and du adds up the files it can reach, so the two can disagree.
- [How do I find which process is using a port on Linux?](https://valakes.com/learn/process-on-port/): Run sudo ss -tulpn and look for the port in the Local Address column. The last column names the program and its PID. lsof -i :PORT answers the same question for one port. Without sudo `ss` still lists every listener but names only your own processes.
- [How do I read logs on a Linux server?](https://valakes.com/learn/read-server-logs/): Use tail -f to watch a log file live, grep -C 3 to see a match with the lines around it, and zgrep for the rotated copies that are compressed. For a service that logs to systemd, run journalctl -u name --since "1 hour ago". You need sudo or the right group for the journal.
