Most of us are aware of the command “free“ which is used to check the RAM usages in the server. First of all, let us familiarize about the output of that command. The image shown is a sample output of the command.
In this example, the total amount of available memory is 12259156 KB. 3947084 KB is used by processes and 8312072 KB is free for other applications. Do not get confused, as the first line shows that 204584 KB is free! It is clear from the example that most of the memory used is for buffers and cache.
Familiarizing with some terms related to cache:
When a process like read or write a file is executed, a copy of the file is being modified in the main memory at the back end. Actually this scenario is performed by the kernel of the OS. So when the read or write process is executed, the kernel first looks for the copy of the file. And if no such copy exists it will create that copy of the file from the disk and writes back changes, whenever the modification is performed. So the memory taken up by these copies is called cached memory or page cache.
Clean Cache Page and Dirty Cache Page:
In this case, the kernel allocates one new page of cache memory and fills it with contents to be read out from the disk. When the user only reads the file the page will be marked as a ‘Clean’ cache page and when the user writes the file then that page will be marked as ‘Dirty’ cache page.
The memory issues in the Linux servers can be overcome by controlling the usage of page cache from the total memory in the server. Controlling of the page cache is done by changing the page cache kernel parameters. The lower the percentage, the more the system favors reclaiming unmapped page cache memory over mapped memory. High values (like the default value of 100) are not recommended for databases.
Let’s check how we can limit the Page Cache or Cache Memory size.
Page cache phenomenon can consume as much memory as available in the machine for executing a process. It could be controlled only dynamically. And there is no particular kernel parameter to directly control page cache size. We could only limit the growth of page cache by tuning some configurable kernel parameters related to the page cache.
==> vm.vfs_cache_pressure (default = 100)
The parameter is used to control the tendency of the kernel to reclaim memory. Lowering this parameter causes the kernel to prefer and retain dentries and inodes caches and increasing this parameter beyond 100 causes the kernel to prefer to reclaim dentries and inodes.
==> vm.dirty_background_ratio ( default = 20)
The parameter is used to indicate the percentage of a system memory. Lowering this parameter will cause the pdflush to write away the dirty data sooner, and it will limit the size of the page cache.
==> vm.dirty_ratio (default=40)
The parameter is used to determine the percentage of the number of pages at which a particular process writes out the data for making the Dirty Cache page (Dirty data). Lowering this parameter will cause that particular process to write the Dirty data earlier, which will limit the page cache size.
==> vm.dirty_expire_centisecs ( default = 3000 , mentioned in milliseconds)
The parameter is used to determine the expiration time for the Dirty pages created so that they could equip themselves to be flushed out by the pdfush. Lowering the value will make more dirty pages to be eligible for flushing out, which would limit the page cache size.
==> vm.swappiness ( default=60)
The parameter is used to determine how soon you want to swap out the data during a process. The Higher parameter will cause the server more likely to swap and the lower parameter will cause the server less likely to swap. Thus, it will write data faster out of the disk and it will limit the Page cache size.
To change parameters dynamically on a running machine
Use the following command.
echo “500” > proc/sys/vm/vfs_cache_pressure
If you want persistent configuration make the modification to /etc/sysctl.conf.
sysctl -w vm.vfs_cache_pressure=”500″.