Skip to content

Linux Foundations / Manage a Linux host ยท Step 38 of 50

Lesson: Monitoring memorySwitch
โ† Linux Foundations

System administration

Monitoring memory

Understand free, buff/cache, available memory and swap.

Linux memory figures often alarm newcomers because "free" looks tiny. Usually that is healthy.

free

bash
$ free -h
               total        used        free      shared  buff/cache   available
Mem:           7.7Gi       2.9Gi       400Mi       200Mi       4.4Gi       4.5Gi
Swap:          2.0Gi          0B       2.0Gi

-h prints human-readable units.

  • free is memory not used for anything at all
  • buff/cache is memory holding file data and kernel buffers, much of which can be reclaimed
  • available estimates how much new programs could use without swapping

Look at available, not free. The kernel uses spare memory as cache to avoid slow disk reads, and gives it back when programs need it.

The same figures are in /proc/meminfo:

bash
$ grep -E 'MemTotal|MemAvailable' /proc/meminfo
MemTotal:        8039212 kB
MemAvailable:    4718592 kB

Swap

Swap is disk space the kernel can use to hold memory pages that do not fit in RAM. Some swap in use is not a problem by itself. Constant movement is. Run:

bash
vmstat 1 5

The si and so columns show memory swapped in and out per second. Values that stay well above zero, together with low available memory, mean the system is short of RAM. swapon --show lists the swap areas.

Who is using it

bash
$ ps -eo pid,rss,vsz,comm --sort=-rss | head -3
    PID   RSS     VSZ COMMAND
   1893 512300 3401220 firefox
    912 168000 2201000 dockerd

RSS (resident set size) is the physical memory the process has in RAM, in KiB. VSZ is all the address space it has mapped, much of which may never be used. Shared pages count in the RSS of each process, so adding the column up overstates the total.

When memory runs out

If the kernel cannot free enough memory, the OOM killer (out of memory) ends a process to recover. If a program vanishes unexpectedly, check the kernel log:

bash
sudo dmesg | grep -i 'out of memory'

On systemd machines, journalctl -k shows kernel messages too.

Try it

Run free -h and note free and available. Press M in top to find your biggest processes. The processes and cron lab is a place to try these commands.

Check your understanding

Monitoring memory

All 4 sections ยท no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.