Skip to content
$ lynuxpro
← Linux

LESSON 11 OF 26 · INTERMEDIATE · 18 MIN

Memory, CPU and performance

In one minute

  • A way to find out why a server is slow, using a few numbers: load average, memory, swap and disk wait.
  • "The server is slow" is vague. Your job is to turn it into a clear answer: is the problem the CPU, the memory, the disk or the network? Each one needs a different fix.
  • You run a short checklist of commands (uptime, free, vmstat, df, ps). Each command checks one part of the machine. The part that looks overloaded is your bottleneck.
  • 👉 Any time an app is slow, an alert fires for high CPU or memory, or a container keeps restarting.

Think of it like: a doctor's check-up. Temperature, pulse, blood pressure. Each check is quick, and together they point to what is wrong.

Load average: uptime

The load average shows how busy the CPU has been: how many processes were running or waiting for their turn.

👉 The very first number to check on a slow server.

uptime
SHELLbashutf-8 · 1L
 09:42:17 up 3 days,  4:12,  1 user,  load average: 0.08, 0.12, 0.10
OUTPUTterminalutf-8 · 1L

The three numbers are averages over the last 1, 5 and 15 minutes.

How to read it: compare it with the number of CPU cores (find that with nproc).

CoresLoadMeaning
40.5Mostly idle
44.0Fully busy, but keeping up
412.0Overloaded: work is waiting in a queue

Look at the trend too, reading from right to left (oldest to newest):

  • 1.0, 4.0, 8.0 → load is going down.
  • 8.0, 4.0, 1.0 → load is going up right now.

Think of it like: checkout lanes in a shop. 4 lanes (cores) with 4 customers (load 4) is busy but fine. 4 lanes with 12 customers means people are queueing.

Memory: free

free shows how memory (RAM) is being used.

👉 The app is slow, or something keeps getting killed.

free -m
SHELLbashutf-8 · 1L
               total        used        free      shared  buff/cache   availableMem:            3936         912        2210          12         813        2780Swap:              0           0           0
OUTPUTterminalutf-8 · 3L
  • available is the column that matters. It is how much memory apps can still get.
  • buff/cache is memory Linux borrows to speed up file reading. It gives it back instantly when apps need it. So a small free number is normal and healthy.
  • Swap is disk space used as extra memory when RAM is full. Disk is far slower than RAM, so heavy swap use makes everything slow.

Remember it: Ignore "free". Read "available".

When memory truly runs out: Linux's OOM killer (Out Of Memory killer) stops a process to save the rest of the system. You see it in the kernel log:

journalctl -k | grep -i "out of memory"# Out of memory: Killed process 2211 (java) total-vm:... anon-rss:...
OUTPUTterminalutf-8 · 2L

In Kubernetes, the same thing shows up as a pod restart with the reason OOMKilled.

Where is CPU time going? top and vmstat

The %Cpu(s) line in top splits CPU time into types.

It tells you what kind of busy the CPU is, which points to the cause.

FieldMeaningIf it is high
usUser programs (your apps)Your app is busy: look at the app
sySystem (the kernel)Lots of low-level system work
waWaiting for diskThe disk is the bottleneck
idIdleSpare capacity
stStolen by the cloud hostAnother customer on the same cloud machine is using it: consider a bigger instance
vmstat 1 5
SHELLbashutf-8 · 1L
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st 1  0      0 2263040 120400 712600    0    0     3    11  210  402  3  1 96  0  0
OUTPUTterminalutf-8 · 3L

vmstat 1 5 prints one line every second, five times. Watch these columns:

  • r: processes waiting for CPU. More than your core count = CPU pressure.
  • b: processes stuck waiting for disk.
  • si / so: swapping in and out. These should be 0.
  • wa: waiting for disk.

The 60-second checklist

👉 The moment someone says "the server is slow". Run these in order:

uptimefree -mvmstat 1 5df -hps aux
SHELLbashutf-8 · 5L
uptime                          # 1. load vs. coresfree -m                         # 2. available memory, swapvmstat 1 5                      # 3. CPU split, IO wait, swappingiostat -xz 1 3                  # 4. disk utilization and latency (sysstat package)df -h                           # 5. full disksps aux --sort=-%cpu | head      # 6. top CPU usersps aux --sort=-%mem | head      # 7. top memory usersss -s                           # 8. connection countsjournalctl -p err --since -1h   # 9. recent errors
OUTPUTterminalutf-8 · 9L

Then match what you see:

What you seeLikely cause
High load, high usThe app is using lots of CPU
High load, high waSlow or overloaded disk
Low available memory, swappingNot enough memory, or a memory leak
OOM messagesOut of memory: raise limits or fix the leak
High stThe cloud machine is starved by its host

Limits and cgroups

cgroups are a Linux feature that caps how much CPU and memory a program may use.

👉 Every Docker container and Kubernetes pod uses them.

docker run --memory 512m --cpus 1 app:1.0# Kubernetes: resources.limits.memory: 512Mi
OUTPUTterminalutf-8 · 2L

A container that goes over its memory limit is OOM-killed, even if the machine has free memory. Many "random restarts" are just limits set too low.

Real-world scenario: slow API after a deploy

Response times doubled after this morning's deploy. You run the checklist:

uptimefree -m
SHELLbashutf-8 · 2L
  • Load is 7 on a 4-core machine → overloaded.
  • Available memory is low and swap is rising → memory pressure.
  • ps aux --sort=-%mem shows the Java app using far more memory than yesterday.

Conclusion: the new release leaks memory or has its memory set too high. Roll back first, investigate second.

Common mistakes

  • Panicking about low "free" memory. Look at available. Linux uses spare RAM for cache on purpose.
  • Reading load without the core count. A load of 8 is fine on 16 cores and terrible on 2.
  • Restarting before collecting clues. Save the output of top, free and the logs first, or the evidence disappears.

Practice

  1. Read the load average and say whether a 2-core server is overloaded.
  2. From free -m, find how much memory apps can still use.
  3. Run vmstat 1 5 and find the disk-wait column.
  4. Write down the 60-second checklist from memory.

Cheat sheet

CommandWhat it shows
uptimeLoad averages
nprocNumber of CPU cores
free -mMemory and swap
vmstat 1 5CPU split, swap, disk wait over time
topLive processes and CPU breakdown
iostat -xz 1How busy and slow the disks are
journalctl -kKernel messages, including OOM kills

Key takeaways

  • A slow server is slow because of CPU, memory, disk or network. Find which one.
  • Load average only makes sense next to the number of CPU cores.
  • For memory, read the "available" column, not "free".
  • When memory runs out, Linux kills a program (OOM kill).
  • Run the 60-second checklist before you restart anything.