Updating VM Memory Usage Configs
Luke Hebert
- 2 minutes read - 350 wordsLinux’s virtual memory subsystem makes a lot of decisions behind the scenes about when to reclaim page cache, when to flush dirty pages, and when to swap anonymous memory. If your workload is I/O-heavy or memory-sensitive, the defaults may not be ideal. Three kernel parameters give you direct control over that behavior.
Parameters
| Parameter | Default | What it controls |
|---|---|---|
vm.vfs_cache_pressure | 100 | How aggressively the kernel reclaims memory used for directory and inode caches. Lower values keep those caches in memory longer. |
vm.dirty_ratio | 20 | The percentage of total RAM that may be dirty (waiting to be written to disk) before the kernel forces a writeback. |
vm.swappiness | 60 | How strongly the kernel prefers to swap anonymous pages versus reclaiming page cache. Lower values favor keeping anonymous memory in RAM. |
Applying the Settings
Add the following lines to /etc/sysctl.conf or, preferably, a dedicated file under /etc/sysctl.d/ (for example /etc/sysctl.d/99-vm-tuning.conf):
vm.vfs_cache_pressure = 100
vm.dirty_ratio = 20
vm.swappiness = 50Then apply them immediately without rebooting:
sudo sysctl -p /etc/sysctl.d/99-vm-tuning.confVerifying
Confirm the values took effect:
sysctl vm.vfs_cache_pressure vm.dirty_ratio vm.swappinessYou should see output like:
vm.vfs_cache_pressure = 100
vm.dirty_ratio = 20
vm.swappiness = 50When to Adjust
- Lower
vm.swappiness(e.g., 10 or 1): Useful on database servers or any workload where swapping causes noticeable latency. The kernel will reclaim page cache before touching anonymous memory. - Raise
vm.dirty_ratio(e.g., 40): Helps bursty write workloads by allowing more data to sit in memory before a forced flush, reducing I/O stalls. - Lower
vm.vfs_cache_pressure(e.g., 50): Beneficial when your application repeatedly accesses the same set of files and directories, since the kernel will hold onto inode and dentry caches longer.
These settings are system-wide. Test them in a staging environment first, especially if you are running a production database or a latency-sensitive service. A value that helps one workload can hurt another.
Reverting
If the tuning causes unexpected behavior, restore the defaults:
sudo sysctl -w vm.vfs_cache_pressure=100
sudo sysctl -w vm.dirty_ratio=20
sudo sysctl -w vm.swappiness=60Then remove or comment out the lines in your sysctl file so they do not reapply on the next boot.