The free Command

The free command provides information about unused and used memory and swap space on any computer running Linux or another Unix-like operating system.

Memory consists of mainly of random access memory (RAM) chips that have been built into multi-chip modules that are, in turn, plugged into slots on the motherboard (i.e., the main circuit board on a personal computer or workstation). Swap space is is a portion of a hard disk drive (HDD) that is used to simulate additional main memory (i.e., which is used for virtual memory).

The basic syntax of free is

free [options]

free accepts no arguments (i.e., input data) and is commonly used without any options. When used with no options, free presents a small table containing six columns and three rows of data, all expressed in kilobytes.

The first row, labeled Mem, displays physical memory utilization, including the amount of memory allocated to buffers and caches. A buffer, also called buffer memory, is usually defined as a portion of memory that is set aside as a temporary holding place for data that is being sent to or received from an external device, such as a HDD, keyboard, printer or network.

The second line of data, which begins with -/+ buffers/cache, shows the amount of physical memory currently devoted to system buffer cache. This is particularly meaningful with regard to application programs, as all data accessed from files on the system that are performed through the use of read() and write() system calls1 pass through this cache. This cache can greatly speed up access to data by reducing or eliminating the need to read from or write to the HDD or other disk.

The third row, which begins with Swap, shows the total swap space as well as how much of it is currently in use and how much is still available.

Several options are available to change the unit of display for free from its default kilobytes, including -b for bytes, -m for megabytes and -g for gigabytes. Of these, -m is usually the most useful. Thus, for example, to show all of the data in megabytes, the following would be used:

free -m

free will report slightly less memory as being in a computer than the actual total. This is mostly because the kernel (i.e., the core of the operating system) cannot be swapped out (i.e., the kernel always remains in main memory while the computer is in operation), and thus the memory that it occupies can never be freed. There can also be regions of memory that are reserved for other purposes, according to the specific system architecture.

The -t option instructs free to additionally display a fourth line of data containing the totals for physical memory and swap space.

The -s option followed by an integer tells free to keep providing a new, updated output at regular intervals for which the integer indicates the number of seconds. This scrolling output can be terminated by simultaneously pressing the CONTROL and c keys. Thus, for example, the following would provide new data every five seconds and display the output in megabits:

free -ms 5

An alternative to using free with its -s option would be to run it using the watch command, which by default runs a program provided to it as an argument every two seconds after first temporarily clearing the screen. This can make it easier to see changes as they occur because there is no scrolling. The program can be terminated and the screen returned to its former state by using the CONTROL and c key combination. Thus, for example, to display memory utilization every two seconds, the following would be used:

watch free

This command can be made even more useful by using watch's -d (i.e., difference) option, which highlights changes in output, and its -n option followed by the number one to increase the frequency of reports to one per second as follows:

watch -n 1 -d free

More detailed information about total memory and current memory usage can be obtained by reading the proc/meminfo file directly. This can be accomplished, for example, with the cat command (which is commonly used to read text files) as follows:

cat /proc/meminfo

1A system call is a request in a Unix-like operating system made via a software interrupt (i.e., a signal that an even has occurred) by an active process (i.e., an instance of a program in execution) for a service performed by the kernel. Examples of events that cause system calls are requests by an application program for certain services from the operating system or the termination of such programs.

Created July 9, 2006.
Copyright © 2006 The Linux Information Project. All Rights Reserved.