Linux: Using free to view memory usage
One of the most common system administration tasks is checking and monitoring memory utilization in a system and one of the simplest and most effective tools for checking this is the free utility on Linux. As a utility, free is very basic and is just parsing the /proc/meminfo directory to collect this data. You can gather all of this information manually by looking in /proc/meminfo, but this would be more cumbersome.
By default, the free command looks like this:
# free total used free shared buff/cache available Mem: 1016476 152264 702188 6676 162024 697160 Swap: 2031612 0 2031612
In the past, free showed three lines of data but more modern versions use only two for display to make the output less confusing (or more confusing, depending on how you perceive it.) In the default output is in kilobytes but with the b, k, m, g, and t flags we can force it to show in bytes, kilobytes, megabytes, gigabytes or terabytes. We can also use the h flag to show the output in "human readable" form which is auto-adjusting based on the scale.
# free -m total used free shared buff/cache available Mem: 992 148 684 6 159 680 Swap: 1983 0 1983
Using free is extremely simple. Understanding it takes more effort. We will example each column to make sure that we understand what we are seeing.
Total: The total memory installed onto our system. This is the "MemTotal or SwapTotal" fields in /proc/meminfo
Used: The amount of memory generally considered to be "in use". This is a calculated field based on the equation: [used = total - free - buffers - cache]
Free: The amount of memory that is currently unused. This is taken from /proc/meminfo in the "MemFree and SwapFree" fields.
Shared: Normally shows memory used by tmpfs.
Buffers: Shown only with the "-w" wide display option. The amount of memory in use by the kernel buffers as determined by the "Buffers" field in /proc/meminfo
Cache: Shown only with the "-w" wide display option. The amount of memory in use by page cache and slabs. This number is produced by combining the "Cached and Slab" fields in /proc/meminfo
Buff/Cache: Shown only without the "-w" wide display option. Calculated by adding the Buffers and Cache fields.
Available: Estimation of how much memory is available for starting new applications, without swapping. Unlike the data provided by the cache or free fields, this field takes into account page cache and also that not all reclaimable memory slabs will be reclaimed due to items being in use. Taken from the "MemAvailable" field in /proc/meminfo
It is very important to note that in the "used" memory field we remove the amount of memory that is used by buffers and cache as these are not traditionally considered to be used memory but is opportunistic utilization of memory for performance enhancement. Many tools incorrectly report this and show memory utilization vastly higher than it actually is by showing the "free" number rather than the "used" one. Older versions of free made this far less clear.
The free command is, by default, static, but it does not have to be. If you wish to make it repeat automatically it is trivial to do so. The "-s" flag (for "seconds") will cause the command to print output ever x seconds as you declare. It will do this indefinitely until you stop with (with Control-c of course) unless you combine this with the "-c" or "count" flag that will determine the number of times to output.
So for an example, if we want to output free once every second for ten iterations we would use this command:
free -s 1 -c 10
Part of a series on Linux Systems Administration by Scott Alan Miller