While using LINUX there might come a situation when you are willing to install a new application (big in size) and you wish to know for the amount of free memory available on your system. In LINUX, there exists a command line utility for this and that is free command which displays the total amount of free space available along with the amount of memory used and swap memory in the system, and also the buffers used by the kernel.
This is pretty much what free command does for you.
$free [OPTION] OPTION : refers to the options compatible with free command.
As free displays the details of the memory related to your system , its syntax doesn’t need any arguments to be passed but only options which you can use according to your wish.
Using free Command
You can use the free command as:
// using free command $free total used free shared buffers cached Mem: 509336 462216 47120 0 71408 215684 -/+ buffers/cache: 175124 334212 Swap: 915664 11928 903736 /*free command without any option shows the used and free space of swap and physical memory in KB */
When no option is used then free command produces the columnar output as shown above where column:
- total displays the total installed memory (MemTotal and SwapTotal i.e present in /proc/meminfo).
- used displays the used memory.
- free displays the unused memory.
- shared displays the memory used by tmpfs(Shmen i.epresent in /proc/meminfo and displays zero in case not available).
- buffers displays the memory used by kernel buffers.
- cached displays the memory used by the page cache and slabs(Cached and Slab available in /proc/meminfo).
- buffers/cache displays the sum of buffers and cache.
Options for free command
- -b, – -bytes : It displays the memory in bytes.
- -k, – -kilo : It displays the amount of memory in kilobytes(default).
- -m, – -mega : It displays the amount of memory in megabytes.
- -g, – -giga : It displays the amount of memory in gigabytes.
- – – tera : It displays the amount of memory in terabytes.
- -h, – -human : It shows all output columns automatically scaled to shortest three digit unit and display the units also of print out. The units used are B(bytes), K(kilos), M(megas), G(gigas), and T(teras).
- -c, – -count : It displays the output c number of times and this option actually works with -s option.
- -l, – -lohi : It shows the detailed low and high memory statistics
- -o, – -old : This option disables the display of the buffer adjusted line.
- -s, – -seconds : This option allows you to display the output continuously after s seconds delay. In actual, the usleepsystem call is used for microsecond resolution delay times.
- -t, – -total : It adds an additional line in the output showing the column totals.
- – -help : It displays a help message and exit.
- -V, – -version : It displays version info and exit.
Using free command with options
1. Using -b : It just displays the output in unit bytes.
//using free with -b $free -b total used free shared buffers cached Mem: 521560064 474198016 47362048 0 73826304 220983296 -/+ buffers/cache: 179388416 342171648 Swap: 937639936 12210176 925429760 /*everything now displayed is in bytes */
2. Using -k : This option displays the result in kilobytes.
//using free with -k $free -k total used free shared buffers cached Mem: 509336 463084 46252 0 72104 215804 -/+ buffers/cache: 175176 334160 Swap: 915664 11924 903740 /*no change in output if compared to only free command output cause this is the by default format that free uses for the result */
3. Using -m : This option displays the result in megabytes.
//using free with -m $free -m total used free shared buffers cached Mem: 497 452 45 0 70 210 -/+ buffers/cache: 171 326 Swap: 894 11 882 /*everything now displayed is in megabytes */
4.using -g : This option displays the result in gigabytes.
//using free with -g $free -g total used free shared buffers cached Mem: 0 0 0 0 0 0 -/+ buffers/cache: 0 0 Swap: 0 0 0 /*everything now displayed is in gigabytes */
5. Using -t (total) : This option displays an additional line containing the total of the total, used and free columns.
//using free with -t $free -t total used free shared buffers cached Mem: 509336 463332 46004 0 72256 215804 -/+ buffers/cache: 175272 334064 Swap: 915664 11924 903740 Total: 1425000 475256 949744 /*the line containing total is added to the output when -t is used*/
6. Using -s and -o: This option allows you to display the output of free command after a set time gap given by the user. This option requires a numeric value to be passed with it that is treated as the number of seconds after which the output will be displayed.
//using free with -s $free -s 3 -c 3 total used free shared buffers cached Mem: 509336 469604 39732 0 73260 216068 -/+ buffers/cache: 180276 329060 Swap: 915664 11924 903740 total used free shared buffers cached Mem: 509336 468968 40368 0 73268 216060 -/+ buffers/cache: 179640 329696 Swap: 915664 11924 903740 total used free shared buffers cached Mem: 509336 469092 40244 0 73272 216068 -/+ buffers/cache: 179752 329584 /*the above output will be displayed (only 3 times) after every 3 seconds */
Now, with -s you can only specify the time gap but not the number of times you want the output to be displayed. For this, -c is used along with -s specifying the number of times the output will be displayed.
7. Using -o : This option makes the buffer/cache line go away from the output as shown below.
//using free with -o $free -o total used free shared buffers cached Mem: 509336 463588 45748 0 72376 215856 Swap: 915664 11924 903740 /*now the output doesn't have the buffer line in it */
That’s all about the free command.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.