Kernel cache pressure and swappiness
Adjust your server’s cache pressure and tendency to swap (swappiness)
Last updated
Adjust your server’s cache pressure and tendency to swap (swappiness)
Last updated
Now that you have swap enabled. Consider adjusting your server’s cache pressure and tendency to swap (vm.swappiness) by following the guide below.
vfs_cache_pressure – Controls the kernel’s tendency to reclaim the memory, which is used for caching of directory and inode objects. (default = 100, recommend value 50 to 200)
swappiness – This control is used to define how aggressive the kernel will swap memory pages. Higher values will increase aggressiveness; lower values decrease the amount of swap. (default = 60, recommended values between 1 and 60) Remove your swap for 0 value, but usually not recommended in most cases.
To edit, you can add or replace these lines in /etc/sysctl.conf file. For example, if you have low memory until you upgrade, you can try something such as:
This will increase the cache pressure, which may seem somewhat counterproductive since caching is good for performance. However, too frequent swapping reduces your server’s overall performance significantly more. So not keeping as much cache in memory will help reduce swap activity. Also, with vm.swappiness set to 10 or as low as 1, it will reduce disk swapping.
On a healthy server with lots of available memory, use the following:
This will decrease the cache pressure. Since caching is good for performance, we want to keep cached data in memory longer. Since the cache will grow larger, we still want to reduce swapping to not cause increased swap I/O.
To check current values using these commands use:
To enable these settings temporarily without rebooting, use the following commands:
Swap space will give you some breathing room in cases that would otherwise lead to out-of-memory exceptions.
If you are running into OOM (out of memory) errors, or if you find that your system is unable to use the applications you need, the best solution is to optimize your application configurations or upgrade your server.
This article has a reference from Hayden James's Linux blog Why You Should Almost Always Add Swap Space.