The Linux free command is used to display the amount of used and free memory in Linux system. This command deals with the physical memory, swap memory and the memory buffers used by Linux kernel. In this article, we will discuss the Linux free command through some practical examples.
Here is the syntax of this command (from the man page) :
free [-b|-k|-m|-g] [-c count] [-l] [-o] [-t] [-s delay] [-V]
Linux free command examples
1. A basic example
Here is a basic example of free command where it is run without any option.
$ free
total used free shared buffers cached
Mem: 3088092 2870016 218076 0 666912 1732864
-/+ buffers/cache: 470240 2617852
Swap: 1340412 88 1340324
So we see that information related to physical memory, kernel memory buffers and swap memory is displayed in the output. By default the information displayed is in terms of kilobytes(KBs).
2. Display the memory statistics in bytes
To display the memory statistics in bytes, the flag -b can be used along with the free command.
For example :
$ free -b
total used free shared buffers cached
Mem: 3162206208 2957144064 205062144 0 683692032 1778278400
-/+ buffers/cache: 495173632 2667032576
Swap: 1372581888 90112 1372491776
So we see that all the information was displayed in bytes.
3. Display memory in GBs
This can be done using the -g flag with the free command.
For example :
$ free -g
total used free shared buffers cached
Mem: 2 2 0 0 0 1
-/+ buffers/cache: 0 2
Swap: 1 0 1
So we see that memory statistics in terms of GBs were displayed in the output.
4. Show low and high memory statistics in detail
To get a detail information on the low and high of the memory, the flag -l is used.
For example:
$ free -l
total used free shared buffers cached
Mem: 3088092 2872740 215352 0 667980 1732212
Low: 865568 721340 144228
High: 2222524 2151400 71124
-/+ buffers/cache: 472548 2615544
Swap: 1340412 88 1340324
So we see that statistics related to low and high of the memory is displayed in the output.
5. Display memory statistics in MBs
This can be done by using the -m flag with the free command.
For example :
$ free -m
total used free shared buffers cached
Mem: 3015 2737 278 0 618 1637
-/+ buffers/cache: 481 2534
Swap: 1308 0 1308
So we see that memory information is displayed in units of MB.
6. Avoid information related to buffers/cache
In the old format of output, the memory information related to buffer/cache was not displayed. To activate this old format, use the -o flag.
For example:
$ free -o
total used free shared buffers cached
Mem: 3088092 2806636 281456 0 633352 1680980
Swap: 1340412 88 1340324
So we see that information related to buffer cache was not displayed.
7. Continuously repeat the output
In order to get a dynamic view of memory in Linux system, the flag -s can be used followed by [no. of seconds]. This flag makes sure that fresh memory statistics is repeatedly shown on stdout after [no. of seconds].
For example :
$ free -s 1
total used free shared buffers cached
Mem: 3088092 2804684 283408 0 633540 1676672
-/+ buffers/cache: 494472 2593620
Swap: 1340412 88 1340324
total used free shared buffers cached
Mem: 3088092 2804940 283152 0 633540 1677376
-/+ buffers/cache: 494024 2594068
Swap: 1340412 88 1340324
total used free shared buffers cached
Mem: 3088092 2809652 278440 0 633540 1681644
-/+ buffers/cache: 494468 2593624
Swap: 1340412 88 1340324
So we see that the information was shown repeatedly after every 1 second.
8. Repeat the output for specified number of times
This is a more customized form of the example shown in (7) above. Using this we can tell free command to repeat the output after lets say 1 second for 3 times or 5 times etc. The flag used in this case is -c.
For example :
$ free -s 1 -c 2
total used free shared buffers cached
Mem: 3088092 2820688 267404 0 633740 1680228
-/+ buffers/cache: 506720 2581372
Swap: 1340412 88 1340324
total used free shared buffers cached
Mem: 3088092 2825656 262436 0 633740 1685368
-/+ buffers/cache: 506548 2581544
Swap: 1340412 88 1340324
$
So we see that the output was repeated for 2 times.
9. Display total for each column using -t flag
To get the total of every column, the flag -t can be used in the following way:
$ free -t
total used free shared buffers cached
Mem: 3088092 2814620 273472 0 634032 1680528
-/+ buffers/cache: 500060 2588032
Swap: 1340412 88 1340324
Total: 4428504 2814708 1613796
So if you look at the last column you will see the total for each column is displayed.
10. Get version using -V flag
As standard with every command, the version information can be fetched through the -V flag.
For example :
$ free -V procps version 3.2.8
So we see that our version of this command is 3.2.8

