Read

Module 7 · The system, disks and the environment

Memory: free and /proc/meminfo

How much RAM there is, how much is really free, and why 'used' memory is usually good news: reading free and the kernel's own meminfo file.

What you will learn

  • Read the columns of free and pick a sensible unit.
  • Tell cache apart from memory that programs actually hold.
  • Pull specific fields out of /proc/meminfo with grep.

Memory questions come in two flavours. *Is this machine big enough?* and *why is it slow right now?* Both start with free, a command that prints a tiny table of kilobytes. The table is easy to misread, so this lesson spends most of its time on what each column means.

Reading free

~% free
             total       used       free     shared    buffers     cached
Mem:        246908      12164     234744          4          0       2796
-/+ buffers/cache:       9368     237540
Swap:            0          0          0
~% free -m
             total       used       free     shared    buffers     cached
Mem:           241         11        229          0          0          2
-/+ buffers/cache:          9        231
Swap:            0          0          0

Without options the unit is kilobytes. -m switches to megabytes, -k is the default, -b bytes and -g gigabytes (which rounds this 241 MB machine down to zero, so pick the unit that fits). The GNU version of free also has -h for human-readable sizes; BusyBox's does not, so in this VM free -h just prints the usage text.

The Mem: row is the physical RAM. total is what the kernel can hand out (a little less than the installed amount, because the kernel itself is not counted). used and free split that total. buffers and cached are the interesting part: that is RAM the kernel is using to keep recently read disk blocks and files around, so the next read is instant. The kernel gives that memory back the moment a program needs it.

That is why the second row, -/+ buffers/cache, exists. Its used column is memory that programs genuinely hold and could not be reclaimed without killing something; its free column is what is really available to new programs. When someone says *Linux ate my RAM*, they read the first row and forgot the second. A healthy, busy server shows almost no free in row one and plenty in row two.

The source: /proc/meminfo

free does not measure anything itself. It reads /proc/meminfo, a text file the kernel generates on demand with forty-odd lines, each a name, a value and a unit. You can read it with cat, but most of the time you want two or three lines, so grep is the natural tool. The most useful field is MemAvailable: the kernel's own estimate of how much memory a new program could get without pushing anything to swap, cache included. It is the single number to quote when someone asks *how much memory is free?*

~% head -5 /proc/meminfo
MemTotal:         246908 kB
MemFree:          234744 kB
MemAvailable:     232636 kB
Buffers:               0 kB
Cached:             2796 kB
~% grep -E 'MemTotal|MemAvailable|SwapTotal' /proc/meminfo
MemTotal:         246908 kB
MemAvailable:     232636 kB
SwapTotal:             0 kB

Because it is plain text, /proc/meminfo combines well with the tools from module 3. awk '/MemAvailable/ {print $2}' /proc/meminfo gives just the number, ready for a script that warns when memory runs low. free | grep Mem | awk '{print $3/$2*100"%"}' prints the used percentage. Neither needs anything installed: the kernel already did the hard part.

FieldMeaning
MemTotalUsable RAM, minus what the kernel reserves for itself
MemFreeRAM nobody is touching at all; small on a busy system and that is fine
MemAvailableWhat a new program could really get, cache included
Buffers / CachedDisk data kept in RAM to speed things up; reclaimable
SwapTotal / SwapFreeOverflow space on disk, zero in this VM

Commands in this lesson

CommandWhat it does
freeMemory table in kilobytes.
free -mThe same, in megabytes.
cat /proc/meminfoEvery memory counter the kernel exposes.
grep MemAvailable /proc/meminfoThe one number that answers 'how much is free?'
grep -E 'MemFree|MemAvailable' /proc/meminfoSeveral fields at once with an extended regex.

Quiz

  1. In free's output, which row tells you how much memory programs could still get?

    • The 'free' column of the Mem: row
    • The 'free' column of the -/+ buffers/cache row
    • The Swap: row
    • The 'shared' column
  2. What does a large 'cached' value usually mean?

    • A memory leak
    • The kernel is keeping disk data in otherwise idle RAM, which is good
    • Swap is full
    • The machine needs a reboot
  3. Where does free get its numbers?

    • /etc/meminfo
    • /proc/meminfo
    • /var/log/memory
    • The BIOS
  4. Which /proc/meminfo field is the kernel's own estimate of memory a new program could use?

    • MemFree
    • MemTotal
    • MemAvailable
    • Buffers
  5. Why does `free -g` show zeros in this VM?

    • There is no memory
    • The machine has less than 1 GB and the value rounds down
    • -g means 'graphics memory'
    • free needs root for gigabytes

Practice

  1. Save the memory table, in megabytes, to `/root/lab/l62/mem.txt`.

  2. Write only the MemTotal line of /proc/meminfo into `/root/lab/l62/total.txt`.

  3. With a single grep, save both the MemFree and the MemAvailable lines of /proc/meminfo into `/root/lab/l62/avail.txt`.

Open this lesson in the app to do the tasks in a real Linux machine and have them checked.