Monday, May 2, 2016

Buffers and Cached memory in free command.

Buffers and Cached memory in free command.


Explaining the free command outputs:

total: Yes, this is total ram.
used: This is probably the most confused column. This is a mix of application used memory and other 'temporarily' (buffer + cache) used memory that is actually available if needed. So technically the memory is truly being used, but much of this memory is available if an application needs it. The 'temporarily' used memory is borrowed if available by the linux system to help speed up system performance, otherwise the system would have read from disk more often. Much of this type of memory is shown under the 'cached' column. This memory is given up by the linux system if an application need memory.
free: Yes, this pure free and untouched memory.
shared: Memory specifically allocated for use by multiple processes
buffers: Temporary memory that is set aside to help some processes
cache: Memory that is available and 'borrowed' by the operating system to help speed up many linux OS operations. This memory is given up by the system if an application need it.


The difference between the first and the second rows is that the first is row is does not count caches and buffers under the free column, while the second one does. Here is an example:
                    total       used       free     shared    buffers     cached
[1]    Mem:       4028712    2972388    1056324          0     315056     835360
[2]    -/+ buffers/cache:    1821972    2206740


used[2] = used[1] - buffers - cached
free[2] = free[1] + buffers + cached


The first line means:
·         total: Your total (physical) RAM (excluding a small bit that the kernel permanently reserves for itself at startup); that's why it shows ca. 11.7 GiB , and not 12 GiB, which you probably have.
·         used: memory in use by the OS.
·         free: memory not in use.
total = used + free
·         shared / buffers / cached: This shows memory usage for specific purposes, these values are included in the value for used.
The second line gives first line values adjusted. It gives the original value for used minus the sum buffers+cached and the original value for free plus the sum buffers+cached, hence its title. These new values are often more meaningful than those of first line.
The last line (Swap:) gives information about swap space usage (i.e. memory contents that have been temporarily moved to disk).
To actually understand what the numbers mean, you need a bit of background about the virtual memory (VM) subsystem in Linux. Just a short version: Linux (like most modern OS) will always try to use free RAM for caching stuff, so Mem: free will almost always be very low. Therefore the line -/+ buffers/cache: is shown, because it shows how much memory is free when ignoring caches; caches will be freed automatically if memory gets scarce, so they do not really matter.
A Linux system is really low on memory if the free value in -/+ buffers/cache: line gets low.

What is the difference between something that is buffered vs. cached?
A buffer is something that has yet to be "written" to disk. A cache is something that has been "read" from the disk and stored for later use.