Friday, August 31, 2012

Resource Management : Free

The Free command is only of the mostly used command in linux system. It displays amount of free and used memory in the system.

When you execute the free command like, it gives the details of the memory as well as the information about the buffer, shared and cached also.

[root@vx111a ~]# free –m (m option shows the memory in MB)
                  total       used       free     shared    buffers     cached
Mem:          3033        665       2367          0         25        384
-/+ buffers/cache:      255        2778
Swap:         5992          0        5992

The total available memory on the system is 3033 mb, of which 665 mb is being in use and 2367 mb as free. The last line shows you about the swap memory details.

Here are the Columns,

Shared: The Shared column says the amount of memory shared between multiple processes.

Buffers: The Buffers column says about the amount of memory being used the kernel buffers. This is used for performing the disk based operations and to speed them up by performing disk operations like read, write to be severed directly from memory. This is taken back if the applications need more memory.

Cached: This column indicates the amount of memory pages being cached by kernel for faster access .This memory can also be reclaimed for application usage.

Clearing the disk cache
it's very convenient to be able to drop the disk cache. For this, we can use the special file /proc/sys/vm/drop_caches. By writing 3 to it, we can clear most of the disk cache:

echo 3 | sudo tee /proc/sys/vm/drop_caches

-/+ buffers/cache:      255       2778

This line is very important in understanding the memory structure of Linux. This line actually indicates the amount of memory used by applications (255) and memory available to applications (2778). If it's close to zero you've run out of RAM and should act accordingly.

Total Physical Memory Available (RAM)      : 3033 MB
Used Physical Memory                              : 665 MB
Memory used by Kernel Buffers                 : 25 MB
Memory Used by Kernel Cached                 : 384 MB

Memory which is used by the kernel but can be reclaimed:  25 MB + 384 MB = 409 MB
Actual Used Physical Memory (Memory Used by Apps): 2778 - 409 MB = 2369 MB
Actual Free Physical Memory (Memory Available For Apps): 2367 MB + 409 MB = 2776 MB

So Even though the amount of physical memory shown is 2367 MB, the original free memory is 2369 MB.

We can use '-t' with free to get the total available memory, like

[root@vx111a ~]# free -m -t
                  total       used       free     shared    buffers     cached
Mem:          3033        665       2367          0         25        384
-/+ buffers/cache:       255       2778
Swap:          5992          0       5992
Total:          9026        665       8360

The free command does not provide the memory in percentages, we can use

[root@vx111a ~]# free -m | grep Mem | awk '{print ($3 / $2)*100}'

And for swap,

[root@vx111a ~]# free -m | grep -i Swap | awk '{print ($3 / $2)*100}'

We can make the free command to display the statistics of the memory available for every 5 second by using,

free -ms 5

More detailed information about total memory and current memory usage can be obtained by reading the proc/meminfo file directly by using

cat /proc/meminfo  ,Which gives details of the memory.

A better command for getting memory details is,

localhsot:root-tmp $ vmstat -s -S M | grep mem
         4096 M total memory
         3099 M used memory
         2689 M active memory
          184 M inactive memory
          996 M free memory
           15   buffer memory

Happy Learning , More To Come