Linux is smart when it comes to managing memory. Memory that isn’t being used to run applications is put to use as disk caches, used to buffer disk reads and writes (and other things). If the applications demand more memory, Linux will move memory from the disk caches to the application transparently. That leads to improved system performance.
If memory demand exceed the amount of memory available, Linux will swap some of the data in memory to disk, thus freeing up that memory for use. Moving data between disk and memory (“swapping”) is orders of magnitude slower than accessing data directly in memory, but it can alleviate a transient memory shortage.
Application memory, disk caching, swap: it’s all very clever, but it does make it harder to understand how much memory is available to applications.
What’s Going On?
The free
command can help, but it’s often poorly understood. It doesn’t have many options, but -h
displays the output in “human” friendly units. Note that older versions of free
don’t support that switch, so just omit it if it is unrecognised.
Here’s the output of free -h
:
$ free -h total used free shared buffers cached Mem: 15G 15G 262M 1.1G 895M 6.9G -/+ buffers/cache: 7.5G 8.0G Swap: 4.7G 112K 4.7G
The first line tells us:
- the system has 15G of memory (“total”)
- almost all of it is in use (“used” and “free”)
It’s the second line, however, that is arguably more useful. That tells us:
- the total amount of memory in use minus that used for buffers and caches – in other words, the amount of memory the operating system and applications are using (“used”)
- the total amount of memory that is free plus that used for buffers and caches – in other words, how much memory is, or could be, available to applications (“free”)
The final line (“swap”), as you might expect, tell us how much swap space is configured, how much of that swap space is used, and how much is available.
Swap
In the example above, the system is in good health from a memory perspective at this moment in time. Little swap is in use, and about half the total memory is being used by applications. If necessary, Linux can make an additional 8GB available to applications.
The issue with swap usage isn’t so much that some memory has been swapped to disk; rather, it is the process of actually accomplishing the swapping that slows systems down. You can see how much a system is swapping with the vmstat
command:
$ vmstat 1 -Sm procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 0 0 0 300 938 7204 0 0 7 18 1 0 60 2 38 0 0 0 0 0 301 938 7204 0 0 0 76 563 2215 8 1 91 0 0 0 0 0 301 938 7204 0 0 0 0 475 2056 7 1 92 0 0 ^C
This command will output one line per second until it is interrupted with control-C. The columns of interest are the “si” (“swap in”) and “so” (“swap out”) lines. If they are predominantly zeros, the system is not swapping heavily.
Memory management within Linux is a complex subject, and I won’t pretend to have covered a lot of detail here; however, I hope I have made it a little easier to understand.
Could This Linux Tip Be Improved?
Let us know in the comments below.