Thursday, January 27, 2011

Clean Cache Process from Phisical Memory in Linux OS

As already known, Linux operating system knows with mature memory management facility, where the system will use maximum extra space in the memory as cache storage. The free area in memory should be accessible to be used by running processes in the system. But unfortunately there are some Linux distributions which doesn’t automatically include this facility, as now I use openSUSE 11.2 32bit and 64bit, maybe the developers are thinking cache memory eliminate is not very important compare to provide an empty memory space for running system optimally.

Currently I’m maintaining a server with 8GB memory, when the server first time startup or restarts, it can be seen by put "top" command to shell, then it will be see very much availability of space in physical memory. It could be said from 8GB available memory is only use 500MB, but when the operating system has been running for more than 2 days with applications that run on the Java platforms and MySQL database it has to be ascertained the server will crash because it ran out of empty space in memory while the JVM cache management and database cache system already sett to maximum configuration in order not to spend to much space in physical memory, and at last it requires to manual restart periodically.

But this is not a concern anymore now, when I found a command to clear the cache from memory. When I search it in Google and will be discussed in the following note.

There are 3 kinds of cleaning cache methods in memory, which can only be distinguished by the numbers 1, 2 or 3, the first is to clean pagecache (pagecache is information stored in memory which result from process of reading and writing data to disk), the second clean dentries and inodes (dentries and inodes are part of the Virtual File System in Linux), the third cleaning pagecache, dentries and inodes:

echo 1 > /proc/sys/vm/drop_caches
echo 2 > /proc/sys/vm/drop_caches
echo 3 > /proc/sys/vm/drop_caches

But do not forget to write "sync" command in the shell before writing the above three kinds of commands. From the three script mentioned above to obtain maximum results we recommend use the "echo 3 > /proc/sys/vm/drop_caches", and by using "top" command we can see that there is impairment results in "Memory Used" section from generated "top" command interface.

After knowing the useful of these commands then we can use it when the memory full in server, but this would be done manually? The response is "It can!”, imagine if it can not be done automatically it will lead to people who served as server administrator to suffer. Therefore it's good to use crontab in scheduling command is executed. Simply by inserting the commands written below to crontab editor then the function to clear the memory cache has to run every hour 6 am and 6 pm.

0 6,18 * * * sync
1 6,18 * * * echo 3 > /proc/sys/vm/drop_caches

In this article did not explain more about crontab and how to edit this function, because to know more about this can be found in article

Maybe for now, only this knowledge can be shared with readers, and hopefully may be useful, if there is a shortage or entries which can improve please feel free to add comments.