RHEL 6 – Controlling Cache Memory / Page Cache Size

RHEL-MemWhat is Page cache ? 

When a user process reads or writes a file, it is actually modifying a copy of that file in main memory. The kernel creates that copy from the disk and writes back changes to it when necessary. The memory taken up by these copies is called cached memory, in other words we call it as page cache.
 
Cached memory will be consumed whenever a user process initiates a read or write. The kernel will look for a copy of the part of the file the user is acting on, and, if no such copy exists, it will allocate one new page of cache memory and fill it with the appropriate contents read out from the disk. If the user only reads the file, this page will be marked as a “clean” cache page. However, as soon as the user writes the file, the page will be marked “dirty.” A kernel thread which appears in ps called “pdflush ( upto kernel version 2.6.31) / flush ( for kernel version 2.6.32 or later)” will wake up periodically and copy all of the pages marked dirty back to the disk, then mark them as clean again.

How do we see the current cache memory usage ? 

Look at the cached column from the below output
 

[root@gurkullinux ~]# free -m
total used free shared buffers cached
Mem: 15976 15195 781 0  167     9153
-/+ buffers/cache: 5874 10102
Swap: 2000 0 1999
[root@gurkullinux ~]#

 

 How to limit the Page Cache or Cache Memory size in Red Hat Enterprise Linux ( RHEL) 6?

 
In RHEL 6 page cache is dynamically controlled and it can take as much memory as available in the machine. The important point here is, in RHEL there is no kernel parameter to directly control page cache size, but all we can do here  is limit the growth of  page cache  by tuning some configurable kernel parameters. 

 

What are configurable kernel parameters that controls the size of the page cache ( or cache memory ) size?

 
vm.vfs_cache_pressure (default = 100)
 
it controls the tendency of the kernel to reclaim memory. Decreasing vfs_cache_pressure causes the kernel to prefer to retain dentry and inode caches. Increasing vfs_cache_pressure beyond 100 causes the kernel to prefer to reclaim dentries and inodes
 
Increasing this value ( e.g. 500) will make your cause for more frequent reclaim of cache memory and limit the size of page cache.
 
vm.dirty_background_ratio ( default = 20)
 
Indicates the percentage of a system memory, the number of pages at which the pdflush background writeback daemon will start writing out dirty data. Decreasing this number will cause the pdflush start wrtiting out the dirt data sooner, and it will limit the size of page cache.
 
vm.dirty_ratio (default=40)

Indicates the percentage of the number of pages at which the processes writes out their own dirty data. Decreasing this number will cause the processes to write out the dirty data sooner, and that will limit the page cache size.
 
vm.dirty_expire_centisecs ( default = 3000 , mentioned in milliseconds)
 
Indicates the expire time for the dirty pages , so that they became eligible to be flushed out by the pdflush. Decreasing this value wil make the more dirty data pages eligible for flush out and that will limit the page cache size.
 
vm.swappiness ( default=60)

Indicates, how soon you want to swap out the data, the more the value the more likely to swap. Decreasing this values will cause the machine to less likely to swap and thus more like to write data out to disk. And that will limit the Page cache size.
 
Note: Make this Value Zero , doesn’t stop your system from Swapping.
 
 

How to Change these kernel Parameters ?

 
If you want to change parameters dynamically on a running machine you can use the command similar to
 
echo “500” > proc/sys/vm/vfs_cache_pressure
 
If you want persistent configuration ( the below below command will make modification to /etc/sysctl.conf)
 
sysctl -w vm.vfs_cache_pressure=”500″
 
 
If you are experiencing the problem like your entire page cache occupying the physical memory you should initially consider to tune the parameters , phase by phase, changing the values by 15% to 20%. But don’t change them at one shot altogether, because modifications to these values could affect the performance of the system in significant manner either in positive or negative manner.
 
And don’t make multiple changes at a time until you confirm previous change doesn’t impact your systems negatively.
 
 
 
 
 
 
 
 

Ramdev

Ramdev

I have started unixadminschool.com ( aka gurkulindia.com) in 2009 as my own personal reference blog, and later sometime i have realized that my leanings might be helpful for other unixadmins if I manage my knowledge-base in more user friendly format. And the result is today's' unixadminschool.com. You can connect me at - https://www.linkedin.com/in/unixadminschool/

3 Responses

  1. Manoj K says:

    Hi,

    Thanks for the valid information. By default in all RHEL 6 systems I am putting the value as follows and found it is very effective.

    vm.swappiness=0
    vm.dirty_background_ratio=3
    vm.dirty_ratio=15
    vm.dirty_expire_centisecs=500
    vm.dirty_writeback_centisecs=100

    What is you opinion on this

    Thanks and Regards,
    Manoj K

  2. JC says:

    “vm.dirty_expire_centisecs ( default = 3000 , mentioned in milliseconds)” – Isn’t the number actually hundreths of a second, not thousandths? IE the default is 30 seconds.

  3. Phoenix_fr says:

    Hi Ramdev ! Thanks for the article.
    I think there’s a mistake on how to change the kernel Parameters.
    1) AFAIK the ‘sysctl -w’ command does not make persitent changes (next reboot, all your tuning would be lost).
    Man page says: ”
    sysctl is used to modify kernel parameters at runtime.
    (…)
    -w Use this option when you want to change a sysctl setting

    I did confirm this behaviour with a Red Hat 6 VM

    2) using the ‘echo’ command works, but is not wise as there’s no control on what you write (sysctl will warn you in case of bad argument and so on …)

    Regards.

What is in your mind, about this post ? Leave a Reply

Close
  Our next learning article is ready, subscribe it in your email

What is your Learning Goal for Next Six Months ? Talk to us