How to clean caches used by the Linux kernel

36,076

Solution 1

You may want to increase vfs_cache_pressure as well as set swappiness to 0.

Doing that will make the kernel reclaim cache faster, while giving processes equal or more favor when deciding what gets paged out.

You may only want to do this if processes you care about do very little disk I/O.

If a network I/O bound process has to swap in to serve requests, that's a problem and the real solution is to put it on a less competitive server.

With the default swappiness setting, the kernel is almost always going to favour keeping FS related cache in real memory.

As such, if you increase the cache pressure, be sure to equally adjust swappiness.

Solution 2

The contents of /proc/meminfo tell you what the kernel uses RAM for.

You can use /proc/sys/vm/vfs_cache_pressure to force the kernel to reclaim memory that is used for filesystem-related caches more lazily or eagerly.

Note that your application may only benefit from tuning this parameter if it does little or no disk I/O.

Solution 3

You might find John Nilsson's answer to my Question useful for purging the cache in order to test whether that is related to your problem:

sync && echo 1 > /proc/sys/vm/drop_caches

Though I'm guessing the only real difference is 1 vs 3

Share:
36,076
Guss
Author by

Guss

I'm a self taught software developer, system administrator and all-round code-guy. I've been doing software development, QA, developer support and system and even some graphic design work for as long as I can remember (going back 30 years), both on commercial projects as well as open source and free software, and I enjoy both. Coding is fun, that's why it is worth doing - I hope it never becomes a chore :-)

Updated on February 10, 2020

Comments

  • Guss
    Guss about 4 years

    I want to force the Linux kernel to allocate more memory to applications after the cache starts taking up too much memory (as can be seen by the output of 'free').

    I've run

    sudo sync; sudo sysctl -w vm.drop_caches=3; free
    

    (to free both disc dentry/inode cache and page cache) and I see that only about half of the used cache was freed - the rest remains. How can I tell what is taking up the rest of the cache and force it to be freed?