Check Disk, Memory and CPU Usage on Linux
An alert says your web server is slow, or a deploy fails with No space left on device. This lesson shows how to check disk, memory and CPU usage on Linux with df, du, free, uptime and top, and how to name the process behind a problem.
Disk: how full, and what is filling it
A filesystem is a disk, or part of one, formatted to hold files and mounted into the Linux tree (lesson 5). df, short for disk free, lists each one; -h prints sizes such as 49G, in powers of 1,024. An Ubuntu server prints something like this (trimmed, illustrative):
df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 49G 41G 5.7G 88% /
Read Use% together with Mounted on: here the root filesystem, /, is 88% full. Used plus Avail is a little less than Size because ext4, the usual Linux filesystem, reserves 5% for root by default, so root's services can still write.
df says which filesystem is full, not why. du, short for disk usage, adds up the size of folders. This lists the folders one level below /var, and sort -h orders them by size (lesson 8). You should see one line per folder, smallest first, with the total last (trimmed, illustrative):
sudo du -h --max-depth=1 /var | sort -h
96M /var/cache
1.4G /var/lib
19G /var/log
21G /var
/var/log holds 19G, so old logs are filling the disk (lesson 13). Without sudo, du prints Permission denied for folders it cannot read and undercounts. In a folder of your own, du -sh * gives one total per item, though * skips names starting with a dot.
Inodes
Each file also needs an inode, the record of its owner, permissions, size and where its data sits, though not its name. On ext4 the number of inodes is set when the disk is formatted, so millions of tiny files can use them all. df -i counts them; this server still has plenty (illustrative):
df -i
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sda1 3276800 412345 2864455 13% /
At 100% IUse%, creating a file fails with No space left on device, even though df -h still shows free space. Find the folder holding the most files, often a cache or session folder, and delete what is no longer needed.
Memory: used, free and available
free shows how the machine's RAM and swap are used right now, and -h prints units such as Mi and Gi, short for mebibytes and gibibytes. On the same server you should see something like this, with your own numbers in place of these illustrative ones:
free -h
total used free shared buff/cache available
Mem: 7.7Gi 2.4Gi 1.1Gi 210Mi 4.5Gi 5.3Gi
Swap: 2.0Gi 64Mi 1.9Gi
free is RAM holding nothing. buff/cache is mostly the page cache: copies of recently read files, so the next read skips the disk (lesson 1). The kernel drops cache when programs need memory, so low free is normal: empty RAM does no work.
available is the kernel's estimate of the memory new programs can get without swapping: free RAM plus cache it can drop. Watch this number. In procps-ng 4.0.1 and later, the package behind free and top, used is total minus available.
Swap is disk space holding memory not used lately (lesson 1); a little in use is normal. Memory pressure means available near zero and swap used growing. When both run out, the kernel's OOM (out of memory) killer ends a process, and sudo journalctl -k | grep -i 'out of memory' finds its log line.
CPU: load average, top and iowait
uptime prints one line: the current time, how long the machine has been up, how many users are logged in, and three load averages. It takes no options and needs no sudo. On the busy server from the examples above it prints this (illustrative):
uptime
10:15:42 up 12 days, 3:04, 2 users, load average: 5.84, 4.12, 2.03
The load average is the average number of processes running, waiting for a CPU, or waiting in state D, usually for the disk (lesson 10), over the last 1, 5 and 15 minutes. It is a count, not a percentage, so compare it with the number of cores, which nproc prints: 4 here.
If it is all CPU work, 5.84 on 4 cores means every core is busy and nearly 2 processes are waiting. A 1-minute figure well above the 15-minute one means the load is rising.
top shows the same load line, then a live summary and the busiest processes, refreshed every few seconds; press q to quit. Three of its lines (trimmed):
%Cpu(s): 91.3 us, 4.2 sy, 0.0 ni, 3.9 id, 0.4 wa, …
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
2210 app 20 0 6012340 1.6g 24576 R 372.0 20.8 412:07.33 java
In the %Cpu(s) line, us is user programs, sy the kernel and id idle. wa, iowait, is time a CPU sat idle while a process waited for the disk or other input and output. 91.3 us with 3.9 id means the CPU is saturated: always busy.
High wa means processes are waiting for the disk, so a faster CPU would not help. The kernel's documentation calls iowait unreliable, so treat it as a hint.
%CPU is the share of one core used since the last refresh, so a program with several threads can pass 100%: java's 372.0 is about 3.7 cores. RES is the RAM it holds. htop, a friendlier top, shows the same with a bar per core.
The diagram circles the key numbers: free is low but available high, which is healthy, while us is in the red.
Finding the culprit
To find the process behind a red gauge, sort top's list: P by %CPU, the default, or M by %MEM. For a snapshot to paste into a ticket, sort ps aux by memory, largest first (the minus sign). It prints the header and the biggest (trimmed, illustrative):
ps aux --sort=-%mem | head -n 2
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
app 2210 2.3 20.8 6012340 1677720 ? Rl Nov20 412:07 java -jar app.jar
java's %CPU is 2.3 here but 372.0 in top: as lesson 10 warned, ps averages CPU over a process's whole life, while top shows what is busy now. Then systemctl status 2210 shows which service owns that PID (lesson 12), and its logs say what it is doing (lesson 13).
Quick triage: which gauge is in the red?
Check the gauges in the same order every time, so you do not stop at the first odd number. Notice that the flowchart always ends with one process.
Load comes first because one number shows whether work is queuing at all. Memory comes before disk space because heavy swapping also shows up as waiting for the disk. If an error already says No space left on device, go straight to df. The table lists healthy and warning readings.
Then act on what you found:
- Disk full: delete or rotate what du found, such as old logs. If df still shows the space as used, a running process holds the deleted file open; restart it.
- Memory pressure: restarting a leaking service buys time; the lasting fix is in its code, a memory limit or more RAM.
- CPU saturated: for expected work, add cores or machines (scaling up versus out); stop a runaway with kill, politely first.
Check yourself
free -h shows free 300Mi but available 5.1Gi. Is the server short of memory?
No. Most of the RAM holds page cache, which the kernel hands back on demand; available is the number that counts.
A 2-core server has a load of 6.0, but top shows 70 wa and 20 id. Would a faster CPU help?
No. The CPUs are mostly idle; Linux counts processes waiting for the disk in the load, so look at disk activity.
Writes fail with No space left on device, but df -h shows 40% used. What next, and why?
df -i: the filesystem may be out of inodes, one per file, often after millions of tiny files.