Pages

Saturday, October 29, 2011

Available, used and free memory in Linux

free will show the used and available memory. The default output could be like this:

# free
             total       used       free     shared    buffers     cached
Mem:       3792504    1499452    2293052          0      48460     586436
-/+ buffers/cache:     864556    2927948
Swap:      4200992          0    4200992

The columns have the following meaning:
total: the total amount of memory available
used: the used memory
free: the free memory
shared: memory used by more than one process simultaneously
buffers: used to buffer disk I/O (included by used)
cached: cached files that have been used before (included by used)

The rows have the following meaning:
Mem: the physical available memory
-/+ buffers/cache: this line represents the real used memory without buffers and cache
Swap: size of the swap partition (virtual memory)

If you subtract the used memory for buffers and cache from the used memory you will get the real amount om memory used:

# echo 1499452-\(48460+586436\) | bc -l
864556

The result is showed by the line -/+ buffers/cache and represents the real used memory by users etc. In most cases you need exactly this line to know how much memory is currently consumed. In this case 864556 KB are used and 2927948 KB are available on the test system.
free has only a few options like choosing the unit of the memory size to show: bytes, kilobytes (default), megabytes and gigabytes:

# free -b
             total       used       free     shared    buffers     cached
Mem:    3883524096 2446561280 1436962816          0   75014144  654159872
-/+ buffers/cache: 1717387264 2166136832
Swap:   4301815808          0 4301815808
# free -k
             total       used       free     shared    buffers     cached
Mem:       3792504    2389220    1403284          0      73256     638828
-/+ buffers/cache:    1677136    2115368
Swap:      4200992          0    4200992
# free -m
             total       used       free     shared    buffers     cached
Mem:          3703       2333       1370          0         71        623
-/+ buffers/cache:       1637       2065
Swap:         4102          0       4102
# free -g
             total       used       free     shared    buffers     cached
Mem:             3          2          1          0          0          0
-/+ buffers/cache:          1          2
Swap:            4          0          4

Cached files are files that can be used multiple times but only need to read once from disk. E.g. if you play a movie from harddisk first time, it will be copied into the memory (cached). If you play the movie the second time, it will be read from memory:

# free
             total       used       free     shared    buffers     cached
Mem:       3792504    1536832    2255672          0       1800     140168
...
# mplayer exiting_movie.avi
# free
             total       used       free     shared    buffers     cached
Mem:       3792504    1572740    2219764          0       6800     167536
...
# sync && echo 3 > /proc/sys/vm/drop_caches
# free
             total       used       free     shared    buffers     cached
Mem:       3792504    1539572    2252932          0       1672     140112
...

In the example above you see that the buffer and cache will fill while loading and playing the movie. After dropping the buffer and the cache all the prior used memory is available again (in this case even more). The buffer can be dropped by syncing the filesystems (sync) and the cache can be dropped by advising the kernel to drop the cache (echo 3 > /proc/sys/vm/drop_caches).
free has also a delay switch. With it free will refresh the output every n seconds:

# free -s 10
...

In the example above free will update the output every 10 seconds.

No comments:

Post a Comment