How do I check memory and CPU usage on a Linux server?
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.
Last updated
Commands checked on Ubuntu 24.04.5 LTS (the ubuntu:24.04@sha256:562111b88caa79c71391c0c4d3c2114a7d5c35915c20561fd893cb1e07929e6b
container image), .
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:
$ 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:
totalis the memory the machine has.freeis 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/cacheis that cache. The kernel gives most of it back when a program asks for memory.availableis 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:
$ 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:
$ 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:
$ 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:
$ 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 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 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 covers the other logs.
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
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 manpages.ubuntu.com
- ps(1) on Ubuntu 24.04, --sort and the rss and pcpu fields manpages.ubuntu.com
- top(1) on Ubuntu 24.04, batch mode manpages.ubuntu.com
- vmstat(8) on Ubuntu 24.04 manpages.ubuntu.com
- uptime(1) on Ubuntu 24.04, the load averages manpages.ubuntu.com
- The /proc file system, kernel documentation (MemAvailable in /proc/meminfo) www.kernel.org
Related articles
The app is free.
Get notified at launchmacOS is coming to the Mac App Store. iOS and Android later.