Playing ‘Hide and Seek’ game with memory under linux

0

Some time you may have noticed that free -m command isn’t showing true stat.

             total    used   free  shared  buffers  cached
Mem:          4086   2302   1784       0        4      65
-/+ buffers/cache:   1232   2854
Swap:         2048      0  
2048

In first sight you can say that Total 2302 MB memory is used. If you have some experience with linux memory system or free command results, then you answer will be total 1232 MB memory is used  and 1072 MB is cached.

Then, you may also want to execute ps awfux command to verify your answer and oh… you figure that only 400 MB memory should be used according to process table. So where is remaining memory ? Lets find that.

Linux page/buffer/inode/dentry caches are good and efficient mechanisms of caching and in most cases you want to leave them alone doing their job, which is speeding your computer by using unused RAM for caching.

Accessing /proc/meminfo in your shell should show you current memory allocation details. /proc/slabinfo may give you some inner-sight and clue about kernel memory management.

So now when you have located ‘hidden’ memory and you know that its there for good cause and increase speed of your system, you may want to pause this game, which is advised too.

However, if you just want to see your numbers back or doing some benchmarking/testing then you can ask linux kernel to free cached memory. Please note that these instruction works only under linux command 2.6.16 and above. Try at your own risk and don’t try at all if you are unsure.

To free pagecache:

  • echo 1 > /proc/sys/vm/drop_caches

To free dentries and inodes:

  • echo 2 > /proc/sys/vm/drop_caches

To free pagecache, dentries and inodes:

  • echo 3 > /proc/sys/vm/drop_caches

Since this is a non-destructive operation, and some garbage but dirty objects are not freeable, you should run sync command first in order to make sure all cached objects are freed.

This may take 1-2 minutes and after that you will be back to shell. Try free -m command now. It should show almost same number in used memory as your process list is showing. Its a good idea to set drop_caches to 0 back, so that kernel can continue with its caching mechanism in future. For this:

  • echo 0 > /proc/sys/vm/drop_caches

Whoopee! You won the game!