Linux Basics Lesson 25 of 38
Reading memory
9 minutes
Learning goals
- Read free -h and explain why available counts and free does not.
- Find the same values in /proc/meminfo, with MemAvailable as the number that matters.
- Check the swap area with /usr/sbin/swapon --show and say why the path is needed.
- Tell from the columns si, so and r in vmstat whether a machine is swapping or queuing.
free: which number counts
free -h gives you the overview, in readable units:
free -h
Purpose: Shows how much memory is in use, how much is free and how much programs can still get.
Syntax: free [-h | -m]
Options:
Example:
free -h
total used free shared buff/cache available
Mem: 15Gi 6.7Gi 921Mi 50Mi 8.4Gi 8.3Gi
Swap: 2.0Gi 204Mi 1.8GiRead it from the right. available is what programs can still get without the machine having to swap, here 8.3 GiB. free is memory nobody uses at all, here 921 MiB. It is often small, because Linux uses idle RAM as a cache for files and gives it back when a program needs it. That cache has its own column, buff/cache.
used needs care. procps 4.0.4, the version in Debian 13, calculates used = total - available. That is why the columns do not add up to total: 6.7 + 0.9 + 8.4 is more than 15. Part of the cache is counted twice, once in buff/cache and once in used, because not all of it can be given back. Older versions of procps calculate used differently, so on an old server the same machine can show a different number. available means the same everywhere.
The line Swap: shows the overflow area on the disk. 204 MiB in use is no alarm in itself. It may be old data that nobody has touched for days.
/proc/meminfo: where the numbers come from
free does not measure anything itself. It reads a file that the kernel provides:
cat /proc/meminfo
Purpose: The kernel's own list of memory values, which free also reads.
Syntax: cat /proc/meminfo | /usr/sbin/swapon --show | cat /proc/swaps
Options:
Example:
cat /proc/meminfo
MemTotal: 15728640 kB
MemFree: 943718 kB
MemAvailable: 8703181 kB
Buffers: 307200 kB
Cached: 8500000 kB
SwapCached: 0 kB
Active: 0 kB
Inactive: 0 kB
Active(anon): 0 kB
Inactive(anon): 0 kB
Active(file): 0 kB
Inactive(file): 0 kB
Unevictable: 0 kB
Mlocked: 0 kB
SwapTotal: 2097152 kB
SwapFree: 1887436 kB
Zswap: 0 kB
Zswapped: 0 kB
Dirty: 0 kB
Writeback: 0 kB
AnonPages: 0 kB
Mapped: 0 kB
Shmem: 51200 kB
KReclaimable: 0 kB
Slab: 0 kB
SReclaimable: 0 kB
SUnreclaim: 0 kB
KernelStack: 0 kB
PageTables: 0 kB
SecPageTables: 0 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 0 kB
Committed_AS: 0 kB
VmallocTotal: 34359738367 kB
VmallocUsed: 0 kB
VmallocChunk: 0 kB
Percpu: 0 kB
HardwareCorrupted: 0 kB
AnonHugePages: 0 kB
ShmemHugePages: 0 kB
ShmemPmdMapped: 0 kB
FileHugePages: 0 kB
FilePmdMapped: 0 kB
Unaccepted: 0 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
Hugetlb: 0 kB
DirectMap4k: 0 kB
DirectMap2M: 0 kB
DirectMap1G: 0 kBThe list is long, and in this simulator many lines show 0. Seven of them are enough for you: MemTotal is all the RAM, MemFree the unused part, MemAvailable the part that counts, Buffers and Cached make up most of buff/cache, SwapTotal and SwapFree describe the swap area. The values are in KiB.
On a real server, grep MemAvailable /proc/meminfo picks out the one line. grep is not loaded on this page and answers command not found.
Which swap area is it? swapon --show gets you only bash: swapon: command not found, because the program lives in /usr/sbin, and that is not in the PATH of lerner. It often is not on real servers either. So you give the path, and it works without sudo:
/usr/sbin/swapon --show
NAME TYPE SIZE USED PRIO
/swapfile file 2G 204.8M -2
cat /proc/swaps shows the same thing, in KiB.
vmstat: is the machine really struggling?
free tells you how much is taken. vmstat tells you whether that hurts:
vmstat
Purpose: One line about processes, memory, swap traffic, disk and CPU.
Syntax: vmstat
Options:
Example:
vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- -------cpu-------
r b swpd free buff cache si so bi bo in cs us sy id wa st gu
1 0 209716 943718 307200 8500000 0 0 0 0 0 0 0 0 100 0 0 0si and so are the amounts of memory swapped in from disk and swapped out to disk per second, by default in KiB per second. If they stay above 0, the machine is constantly shuffling data between RAM and disk, and it gets slow. That is real memory pressure. r counts the processes that want to run. Permanently more than there are cores means a queue, and that has nothing to do with memory.
Careful with the first line: For rates like si and so, it shows averages since the last boot, not what is happening right now. On a real server you therefore run vmstat 1 5. It prints five lines a second apart, and you judge the current activity by the lines after the first one. The clock of this simulator stands still between commands, so plain vmstat gives you its one line here, and vmstat 1 5 answers with a hint. A single 0 here is an example value, not proof that a real machine is fine.
Practice
Terminal task 1/3
Someone is worried about the memory of this machine. Show the overview in readable units.
Task 1/3: Someone is worried about the memory of this machine. Show the overview in readable units.
Someone is worried about the memory of this machine. Show the overview in readable units.
Task complete
Solution
free -h Why: Look at the last column first. 8.3 GiB available means that programs have plenty of room, even with only 921 MiB under free.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Fill the gap
How much memory programs can still get is in the column:
How much memory programs can still get is in the column: …
Check works once you have typed an answer.
Terminal task 2/3
free gets its numbers from a file that the kernel provides. Print that file and look for the line MemAvailable.
Task 2/3: free gets its numbers from a file that the kernel provides. Print that file and look for the line MemAvailable.
free gets its numbers from a file that the kernel provides. Print that file and look for the line MemAvailable.
Task complete
Solution
cat /proc/meminfo Why: 8703181 KiB is the 8.3 GiB from free -h. On a real server, grep MemAvailable /proc/meminfo picks out the one line, for example in a script.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Terminal task 3/3
Find out which swap area this machine uses. Plain swapon --show will fail, and the text says why.
Task 3/3: Find out which swap area this machine uses. Plain swapon --show will fail, and the text says why.
Find out which swap area this machine uses. Plain swapon --show will fail, and the text says why.
Task complete
Solution
/usr/sbin/swapon --show Why: command not found does not always mean that a program is missing, only that the shell does not look for it there. cat /proc/swaps is the same thing without the path: it is the file that swapon reads.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Predict the output
Command
swapon --showBefore the command, in your home folder ~
- ~/.bashrc
- ~/.profile
- ~/.secret
- ~/Documents/
- ~/logs/
- ~/notes.txt
- ~/projects/
- ~/archive/
Check works once you have chosen an answer.
Next up: Who is using all that memory, and what happens when it runs out.
Quiz
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
Cards
-
Card 1 of 3
Tap the card or press Space to turn it over.
-
Card 2 of 3
Tap the card or press Space to turn it over.
-
Card 3 of 3
Tap the card or press Space to turn it over.
End of the lesson
Still open: 5 exercises, 3 quiz questions
What you can do now: free -h, cat /proc/meminfo, vmstat
+0 XP in this lesson
- Lesson 1: open
- Lesson 2: open
- Lesson 3: open
- Lesson 4: open
- Lesson 5: open
- Lesson 6: open
- Lesson 7: open
- Lesson 8: open
- Lesson 9: open
- Lesson 10: open
- Lesson 11: open
- Lesson 12: open
- Lesson 13: open
- Lesson 14: open
- Lesson 15: open
- Lesson 16: open
- Lesson 17: open
- Lesson 18: open
- Lesson 19: open
- Lesson 20: open
- Lesson 21: open
- Lesson 22: open
- Lesson 23: open
- Lesson 24: open
- Lesson 25: done
- Lesson 26: open
- Lesson 27: open
- Lesson 28: open
- Lesson 29: open
- Lesson 30: open
- Lesson 31: open
- Lesson 32: open
- Lesson 33: open
- Lesson 34: open
- Lesson 35: open
- Lesson 36: open
- Lesson 37: open
- Lesson 38: open
Cheat sheet
-
free -hmemory overview, available counts -
cat /proc/meminfothe kernel's values, in KiB -
/usr/sbin/swapon --showactive swap areas -
vmstatsi and so show swapping, r the queue
The most important command lines of this module on one A4 page, black and white, to print or keep on your phone.
- Download PDF (PDF, 17 KB)
- View as image (PNG, 44 KB)