How to Configure Swappiness and VFS Cache Pressure in Ubuntu Using sysctl

Memory management in the Linux kernel is designed to maximise physical RAM efficiency by aggressively caching filesystem metadata and disk blocks in unused memory. However, on production Ubuntu database servers, high-throughput container hosts, and desktop workstations, default kernel memory reclamation thresholds can occasionally trigger premature disk swapping or excessive directory inode churn.

Two essential kernel tunables controlled via sysctl govern this behaviour: vm.swappiness and vm.vfs_cache_pressure. By understanding their interactions and tuning them within persistent sysctl configuration files, system administrators can reduce I/O bottlenecks and achieve responsive multitasking.

Understanding vm.swappiness

The vm.swappiness parameter defines the kernel’s relative preference for swapping out anonymous process memory (active application RAM) versus reclaiming filesystem page cache memory:

  • Range: 0 to 200 (on modern Linux kernels 5.8+, previously 0 to 100). The default on Ubuntu desktop and server installations is typically 60.
  • High Values (60–100+): The kernel aggressively swaps idle process pages to disk swap space to preserve more physical RAM for active page caching.
  • Low Values (10–20): The kernel prioritises keeping process memory in physical RAM, only resorting to swap space when RAM is nearly exhausted. This is recommended for low-latency database engines like PostgreSQL and MySQL.
  • Value 0: The kernel completely avoids swapping anonymous memory until free pages and unmapped page cache fall below the kernel zone watermark threshold.

Understanding vm.vfs_cache_pressure

The vm.vfs_cache_pressure parameter controls the kernel’s tendency to reclaim directory and inode cache (dentry and inode objects) compared to page cache and swap:

  • Default Value (100): The kernel reclaims VFS dentries and inodes at a “fair” rate relative to page cache memory.
  • Lower Values (< 100, e.g. 50): Instructs the kernel to preferentially retain directory structure and file metadata in RAM. This significantly accelerates repeated directory lookups, Git repository operations, and file search commands on file servers with abundant RAM.
  • Higher Values (> 100, e.g. 500): Instructs the kernel to aggressively purge cached filesystem metadata from memory to reclaim RAM for user-space applications.

Checking Current Kernel Values in Ubuntu

To inspect your server’s active runtime parameters, query sysctl in your terminal:

sysctl vm.swappiness
sysctl vm.vfs_cache_pressure

Alternatively, you can read the virtual filesystem endpoints directly from the /proc directory:

cat /proc/sys/vm/swappiness
cat /proc/sys/vm/vfs_cache_pressure

Testing Changes Temporarily at Runtime

Before committing tuning adjustments permanently, you can apply them dynamically to test system responsiveness:

sudo sysctl -w vm.swappiness=10
sudo sysctl -w vm.vfs_cache_pressure=50

These commands update active kernel behavior immediately without requiring a system reboot or service restart. However, changes made with sysctl -w will be discarded upon the next system reboot.

Applying Persistent sysctl Tuning Drop-ins

To ensure your memory tuning parameters survive system reboots, create a custom drop-in configuration file inside /etc/sysctl.d/:

  1. Create a new configuration file (for example, 99-memory-tuning.conf):
    sudo nano /etc/sysctl.d/99-memory-tuning.conf
  2. Add your desired tuning parameters:
    # Custom kernel memory management tuning
    vm.swappiness=10
    vm.vfs_cache_pressure=50
  3. Save the file and exit the editor.
  4. Apply the configuration file immediately without rebooting:
    sudo sysctl -p /etc/sysctl.d/99-memory-tuning.conf

Your Ubuntu server will now retain these optimised memory management thresholds across all subsequent kernel boots.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.