On Linux systems, you may sometimes want to get a quick look at the system’s used and unused memory space from the command line. Good news if you’re new to Linux: there’s a built-in command that displays this information: free.

In this article, we’ll cover the basic usage of the free command and some of the important functions it provides. All the commands and usages mentioned in this article have been tested on Ubuntu 16.04LTS.

Linux free command

Let’s look at the syntax of the free command:

free [options]

Copy the code

The man manual for the free command says this:

freeThe command shows the total amount of available and used physical and swap memory on the system, as well as the cache space used by the kernel. This information is taken from
/proc/meminfoIs obtained from.

Let’s take a look at how the free command works with a q&A.

Q1. How to use the free command to view the used and unused memory?

This is easy, you just need to run the free command with no arguments:

free

Copy the code

Here is the output of the free command on my system:

What do these columns mean?

  • total– Total amount of installed memory (equivalent to/proc/meminfoIn theMemTotalSwapTotal)
  • used– Used memory (calculated by:used = totalfreebufferscache)
  • free– Unused memory (equivalent to/proc/meminfoIn theMemFreeSwapFree)
  • shared– Usually the memory used by the temporary file system (equivalent to/proc/meminfoIn theShmem; This parameter is available from kernel 2.6.32. If this parameter is unavailable, it is displayed0)
  • buffers– Memory used by the kernel buffer (equivalent to/proc/meminfoIn theBuffers)
  • cache– Memory used by page caching and Slab allocation mechanisms (equivalent to/proc/meminfoIn theCachedSlab)
  • buff/cachebufferscacheThe sum of the
  • available– The amount of memory that is expected to be used by the newly launched application without counting swap space. withcacheorfreePartially different, this column takes the page cache into account, and not all of the recoverable slab memory can actually be reclaimed, because there may be occupied portions. (equivalent to/proc/meminfoIn theMemAvailable; Available since kernel 3.14, emulated since kernel 2.6.27; On other versions this value corresponds tofreeThis column is the same.

Q2. How do I change the units displayed?

You can change the display unit of memory if necessary. For example, to display memory in megabytes, you can use the -m argument:

free -m

Copy the code

Similarly, you can use -b in bytes, -k in KB, -m in MB, -g in GB, and –tera in TB.

Q3. How do I display readable results?

The free command provides the -h parameter to convert the output to a readable format.

free -h

Copy the code

With this argument, the free command determines which units to display each value of memory in. Such as:

Q4. How do I keep the free command running at regular intervals?

You can use the -s argument to make the free command run continuously at regular intervals. You need to pass a numeric argument to the command line as the number of seconds for this interval.

For example, make the free command run every 3 seconds:

free -s 3

Copy the code

If you want the free command to be executed only a few times, you can specify the number of times with the -c parameter:

free -s 3 -c 5

Copy the code

The above command ensures that the free command is executed every three seconds for a total of five times.

Note: This feature is currently having problems on Ubuntu, so it has not been tested.

Q5. How to make free based on 1000 computational memory instead of 1024?

If you specify free to display memory in MB (with the -m argument), but want to calculate based on 1000, you can do so with the –sj argument. The following figure shows the result with or without this parameter:

Q6. How do I make the free command display the sum of each column?

If you want the free command to display the sum of each column, you can use the -t parameter.

free -t

Copy the code

As shown below:

Notice that the Total line appears.

conclusion

The free command is an extremely useful tool for system administration. It has many parameters that you can customize your output to make it easy to understand and use. We have also mentioned many useful parameters in this article. After the exercise, please go to the MAN manual for more information.


Via: www.howtoforge.com/linux-free-…

By Himanshu Arora

LCTT
Linux China