Show contents
Chapter 4 of 7 · 20 min
Diagnosing a slow server
By the end of this chapter, you'll be able to
- Read the system load and memory usage
- Find what is filling a disk with df, du and find
- Explain why a deleted file's space can stay in use
- Follow a diagnosis method in order: processor, memory, disk
A method before the commands
A server has four main resources: the processor (CPU), the memory (RAM), the disk and the network. For each one, you ask the same questions: is it saturated? Who is using it? Since when?
We fix the cause, then write down what happened so it does not happen again.
Text description of the animation
- On a 4-core machine, a load of 7.8 means tasks are waiting for the processor.
- Available memory is almost zero: the system may start killing processes.
- A full disk blocks writes: databases and logs stop.
- We identify the process that uses the most of the saturated resource.
- We fix the cause, then write down what happened so it does not happen again.
Processor and memory
uptime shows the load average over 1, 5 and 15 minutes: the average number of tasks using the processor or waiting their turn. Compare it with the number of cores, given by nproc: a load that stays above the number of cores means tasks are queuing.
$ uptime
03:12:44 up 41 days, 2:03, 1 user, load average: 7.80, 6.95, 4.10
$ nproc
4
$ free -h
total used free shared buff/cache available
Mem: 3.8Gi 3.5Gi 95Mi 12Mi 310Mi 120Mi
Swap: 2.0Gi 1.9Gi 100Mi
In free, the column that matters is available: the memory really available for new programs, counting the cache that Linux can release. A low free column is not worrying in itself: Linux uses free memory as disk cache. Here, available is at 120 MiB and swap (the disk area used when RAM runs out) is almost full: the machine really is short of memory. ps --sort=-%mem -o pid,%mem,comm | head then shows the biggest consumers.
What is filling the disk?
df -h shows how full each file system is; du measures the size of a folder. On a real server, you start by spotting the full disk, then the heaviest folder:
$ df -h /var
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 40G 40G 0 100% /var
$ du -h --max-depth=1 /var | sort -h | tail -3
1.2G /var/cache
31G /var/log
40G /var
du -h --max-depth=1 gives the size of each subfolder, and sort -h sorts "human" sizes (K, M, G) in the right order. Here, /var/log takes 31 GiB: the logs are the first suspects.
To find big files, find goes through the whole tree. Let's create a small log folder for the example, then list its files with their exact size in bytes (-printf '%s %p\n' prints the size and the path):
mkdir -p logs/old
head -c 3000000 /dev/zero > logs/app.log
head -c 500000 /dev/zero > logs/errors.log
head -c 8000000 /dev/zero > logs/old/app-2026-10.log
find logs -type f -printf '%s %p\n' | sort -n
500000 logs/errors.log
3000000 logs/app.log
8000000 logs/old/app-2026-10.log
-size filters directly by size: +1M keeps files larger than 1 MiB.
find logs -type f -size +1M | sort
logs/app.log
logs/old/app-2026-10.log
Your turn
Worked exercise
Worked exercise: the heaviest folder
In the logs folder created above, write a command that prints the number of files and their total size in kilobytes (1 kB = 1,000 bytes), then a command that prints only the name of the biggest file.
Hint 1
find logs -type f | wc -l counts the files; find logs -type f -printf '%s\n' gives their sizes in bytes, which awk can add up.Hint 2
size path with -printf, sort with sort -n, keep the last line with tail -1, then the second field with cut -d' ' -f2.Show the solution and common mistakes
echo "$(find logs -type f | wc -l) files, $(find logs -type f -printf '%s\n' | awk '{ t += $1 } END { print t / 1000 }') kB"
find logs -type f -printf '%s %p\n' | sort -n | tail -1 | cut -d' ' -f2
3 files, 11500 kB
logs/old/app-2026-10.log
Common mistakes:
- Sorting the output of
du -hwithsort -n: "8.0M" would come before "500K", because-ndoes not understand units. Usesort -h, or sizes without units. - Searching with
ls -lSonly in the current folder: big files are often hidden in a subfolder.findgoes down the whole tree.
Summary
- 1A diagnosis follows an order: processor load, memory, disk, then the culprit process.
- 2The load average is compared with the number of cores (nproc); in free, the available column is what counts.
- 3df -h shows full disks, du -h --max-depth=1 | sort -h the heaviest folder, find -size the big files.
- 4A deleted file that is still open still takes up disk space: truncate it rather than deleting it.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.