terminal.cab

Linux Basics Lesson 25 of 38

guestLevel 1Badges and progress0/100 XP, XP to the next levelStreak0

Reading memory

9 minutes

Learning goals

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:

  • -h readable units such as 8.3Gi
  • -m everything in MiB

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.8Gi

in the glossary: free

Read 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:

  • /usr/sbin/swapon --show the active swap areas, with the path because /usr/sbin is not in the PATH of lerner

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 kB

in the glossary: cat

The 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:

  • si, so swapped in and out per second, the columns that show real memory pressure
  • r processes that want to run right now

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  0

in the glossary: vmstat

si 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.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

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.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

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.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

Predict the output

Command

swapon --show

Before the command, in your home folder ~

  • ~/.bashrc
  • ~/.profile
  • ~/.secret
  • ~/Documents/
  • ~/logs/
  • ~/notes.txt
  • ~/projects/
  • ~/archive/
What does swapon --show print?

Check works once you have chosen an answer.

Next up: Who is using all that memory, and what happens when it runs out.

Quiz

  1. Question 1 of 3free -h shows 921Mi under free and 8.3Gi under available. What do you tell the person who wants to add RAM?

    Check works once you have chosen an answer.

  2. Question 2 of 3In free -h, used, free and buff/cache add up to more than total. Why?

    Check works once you have chosen an answer.

  3. Question 3 of 3204 MiB of swap are in use, and vmstat shows 0 under si and so. How do you read that?

    Check works once you have chosen an answer.

Cards

  1. Card 1 of 3

    Tap the card or press Space to turn it over.

  2. Card 2 of 3

    Tap the card or press Space to turn it over.

  3. 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

Next: lesson 26, Who eats the memory (10 min)

What you can do now: free -h, cat /proc/meminfo, vmstat

+0 XP in this lesson

  1. Lesson 1: open
  2. Lesson 2: open
  3. Lesson 3: open
  4. Lesson 4: open
  5. Lesson 5: open
  6. Lesson 6: open
  7. Lesson 7: open
  8. Lesson 8: open
  9. Lesson 9: open
  10. Lesson 10: open
  11. Lesson 11: open
  12. Lesson 12: open
  13. Lesson 13: open
  14. Lesson 14: open
  15. Lesson 15: open
  16. Lesson 16: open
  17. Lesson 17: open
  18. Lesson 18: open
  19. Lesson 19: open
  20. Lesson 20: open
  21. Lesson 21: open
  22. Lesson 22: open
  23. Lesson 23: open
  24. Lesson 24: open
  25. Lesson 25: done
  26. Lesson 26: open
  27. Lesson 27: open
  28. Lesson 28: open
  29. Lesson 29: open
  30. Lesson 30: open
  31. Lesson 31: open
  32. Lesson 32: open
  33. Lesson 33: open
  34. Lesson 34: open
  35. Lesson 35: open
  36. Lesson 36: open
  37. Lesson 37: open
  38. Lesson 38: open
1 of 38

Next: lesson 26, Who eats the memory (10 min) That's enough for today

Path complete

You finished all 38 lessons of Linux Basics.

Back to the path

Cheat sheet

The most important command lines of this module on one A4 page, black and white, to print or keep on your phone.