How to use the free command on linux

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 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 calls 1 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 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:

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:

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:

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:

________
1 A 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.

Memory Usage

On linux, there are commands for almost everything, because the gui might not be always available. When working on servers only shell access is available and everything has to be done from these commands. So today we shall be checking the commands that can be used to check memory usage on a linux system. Memory include RAM and swap.

It is often important to check memory usage and memory used per process on servers so that resources do not fall short and users are able to access the server. For example a website. If you are running a webserver, then the server must have enough memory to serve the visitors to the site. If not, the site would become very slow or even go down when there is a traffic spike, simply because memory would fall short. Its just like what happens on your desktop PC.

1. free command

The free command is the most simple and easy to use command to check memory usage on linux. Here is a quick example

The m option displays all data in MBs. The total os 7976 MB is the total amount of RAM installed on the system, that is 8GB. The used column shows the amount of RAM that has been used by linux, in this case around 6.4 GB. The output is pretty self explanatory. The catch over here is the cached and buffers column. The second line tells that 4.6 GB is free. This is the free memory in first line added with the buffers and cached amount of memory.

Linux has the habit of caching lots of things for faster performance, so that memory can be freed and used if needed.
The last line is the swap memory, which in this case is lying entirely free.

2. /proc/meminfo

The next way to check memory usage is to read the /proc/meminfo file. Know that the /proc file system does not contain real files. They are rather virtual files that contain dynamic information about the kernel and the system.

Check the values of MemTotal, MemFree, Buffers, Cached, SwapTotal, SwapFree.
They indicate same values of memory usage as the free command.

3. vmstat

The vmstat command with the s option, lays out the memory usage statistics much like the proc command. Here is an example

The top few lines indicate total memory, free memory etc and so on.

4. top command

The top command is generally used to check memory and cpu usage per process. However it also reports total memory usage and can be used to monitor the total RAM usage. The header on output has the required information. Here is a sample output

Check the KiB Mem and KiB Swap lines on the header. They indicate total, used and free amounts of the memory. The buffer and cache information is present here too, like the free command.

5. htop

Similar to the top command, the htop command also shows memory usage along with various other details.

How to use the free command on linux

The header on top shows cpu usage along with RAM and swap usage with the corresponding figures.

RAM Information

To find out hardware information about the installed RAM, use the demidecode command. It reports lots of information about the installed RAM memory.

Provided information includes the size (2048MB), type (DDR2) , speed(667 Mhz) etc.

Summary

All the above mentioned commands work from the terminal and do not have a gui. When working on a desktop with a gui, it is much easier to use a GUI tool with graphical output. The most common tools are gnome-system-monitor on gnome and
ksysguard on KDE. Both provide resource usage information about cpu, ram, swap and network bandwidth in a graphical and easy to understand visual output.

How to use the free command on linux

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected] .

66 thoughts on “ 5 commands to check memory usage on Linux ”

You have explained the topic very well. Thanks for sharing a nice article.

Thanks for these commands. It saved a lots of time.

How about explaining what we are looking for? Most people would know these commands. Waste of time.

Please correct the typo in “RAM Information” section. The command for viewing hardware info about RAM is “dmidecode” and not “demidecode”.
And it also requires root privileges.