While network sysctl tuning gets most of the attention in VPS performance guides, the vm.* kernel parameters have an equally large impact on day-to-day stability. These settings control how the kernel manages memory: when it swaps, how aggressively it writes dirty pages to disk, and what happens when memory runs out. On a VPS with limited RAM — typically 1 GB to 4 GB — getting these values wrong can cause slowdowns, excessive I/O, or unexpected OOM kills. This guide covers the vm.* parameters that matter most and how to tune them for a virtualized environment.
Understanding vm.swappiness
Swappiness controls the kernel’s preference for keeping processes in RAM versus swapping them to disk. The value ranges from 0 to 200:
- 0: Only swap when absolutely necessary (OOM pressure)
- 1: Minimum swapping — good for most VPS workloads
- 10: Recommended for low-RAM VPS (1-2 GB)
- 60: Default on most Linux distributions
- 100: Aggressive swapping — useful for desktop systems
- 200: Maximum swapping — avoid on a VPS
The default of 60 is designed for general-purpose desktops where the kernel should page out less-frequently-used memory to leave room for file caches. On a VPS, this default causes excessive swapping, which translates to high disk I/O and latency. A lower value keeps your application processes in memory where they belong.
# Check current swappiness
cat /proc/sys/vm/swappiness
# Set to 10 for a low-RAM VPS
sudo sysctl -w vm.swappiness=10
# Make permanent
sudo sh -c 'echo "vm.swappiness=10" >> /etc/sysctl.conf'
For a deeper dive into VPS performance tuning, browse our performance optimization guides.
Tuning vm.dirty_ratio and vm.dirty_background_ratio
These two parameters control when the kernel writes dirty pages (modified data in memory that has not yet been written to disk) back to storage.
| Parameter | Default | VPS Recommendation | Meaning |
|---|---|---|---|
dirty_background_ratio | 10% | 5% | Start writing dirty pages to disk in the background |
dirty_ratio | 20% | 10% | Block processes until dirty pages are flushed |
On a VPS, the default values allow too many dirty pages to accumulate before flushing begins. When a large write happens (e.g., a database checkpoint or a file upload), the kernel blocks all processes until the dirty pages are written to disk. This causes visible latency spikes. Lowering both values smooths out I/O by flushing smaller batches more frequently.
# Set conservative dirty page limits
sudo sysctl -w vm.dirty_background_ratio=5
sudo sysctl -w vm.dirty_ratio=10
# Make permanent
sudo sh -c 'cat >> /etc/sysctl.conf <
vm.vfs_cache_pressure: Keeping Inode/Dentry Caches Under Control
The VFS (Virtual File System) cache stores recently accessed directory entries (dentries) and inodes. On a busy VPS with many files (e.g., a WordPress site with thousands of cached pages), this cache can grow large. The vfs_cache_pressure parameter controls how aggressively the kernel reclaims these caches.
- Default (100): Balanced reclamation
- Lower (50): Keep cache entries longer — more memory used, better performance for repeated file access
- Higher (200): Reclaim cache aggressively — frees memory at the cost of more disk I/O
For a web server VPS, setting vfs_cache_pressure to 50 can improve performance by keeping frequently accessed file metadata in memory:
sudo sysctl -w vm.vfs_cache_pressure=50
sudo sh -c 'echo "vm.vfs_cache_pressure=50" >> /etc/sysctl.conf'
OOM Behavior: vm.overcommit_memory and vm.overcommit_ratio
Linux overcommits memory by default: it allows processes to allocate more virtual memory than physical RAM + swap. When a process actually tries to use the memory, the kernel must find pages to free. If it cannot, the OOM (Out-Of-Memory) killer terminates a process.
On a VPS, the default overcommit behavior can lead to unpredictable OOM kills at the worst possible moment. The safest setting is vm.overcommit_memory=2, which disables overcommit and allocates memory strictly based on available RAM + swap:
# Disable overcommit (strict accounting)
sudo sysctl -w vm.overcommit_memory=2
sudo sysctl -w vm.overcommit_ratio=50 # Allow 50% overhead for allocations
# Make permanent
sudo sh -c 'echo "vm.overcommit_memory=2" >> /etc/sysctl.conf'
sudo sh -c 'echo "vm.overcommit_ratio=50" >> /etc/sysctl.conf'
With overcommit_memory=2, the kernel will reject memory allocations that exceed available capacity. Applications that try to allocate more memory than exists will fail immediately with an error instead of causing a random OOM kill later. This is much easier to debug.
vm.min_free_kbytes: Reserving Memory for Critical Allocations
This parameter reserves a minimum amount of free memory for critical kernel allocations. If the system runs out of memory entirely, the kernel cannot even run the OOM killer. Setting a reasonable reserve prevents this deadlock scenario.
# Reserve 64 MB on a 2 GB VPS (roughly 3%)
sudo sysctl -w vm.min_free_kbytes=65536
sudo sh -c 'echo "vm.min_free_kbytes=65536" >> /etc/sysctl.conf'
A good rule of thumb is 3-5% of total RAM. On a 1 GB VPS, set min_free_kbytes to 32768 (32 MB). On a 4 GB VPS, set it to 131072 (128 MB).
Complete vm.* sysctl Configuration for a VPS
Here is a complete set of vm.* parameters tuned for a low-to-mid-range VPS (1-4 GB RAM, SSD storage):
# /etc/sysctl.d/99-vps-vm.conf
# Memory management tuning for VPS
# Reduce swap tendency
vm.swappiness=10
# Flush dirty pages more frequently
vm.dirty_background_ratio=5
vm.dirty_ratio=10
# Keep directory cache in memory longer
vm.vfs_cache_pressure=50
# Strict overcommit policy
vm.overcommit_memory=2
vm.overcommit_ratio=50
# Reserve memory for critical kernel allocations
vm.min_free_kbytes=65536
# Reduce page-cluster size (pages read ahead on swap)
vm.page-cluster=0
Apply with sudo sysctl -p /etc/sysctl.d/99-vps-vm.conf. Monitor memory usage with free -h and vmstat 1 after applying to verify the system behaves as expected.
Monitoring and Validation
After tuning, validate that your changes are having the desired effect:
# Check current values
sysctl vm.swappiness vm.dirty_ratio vm.dirty_background_ratio
# Monitor swap usage
free -h
swapon --show
# Check OOM status
dmesg | grep -i "oom"
# Monitor page faults and swap activity
vmstat 1 10
If you see swap usage increasing after tuning, your swappiness value may still be too high, or your VPS simply needs more RAM. For persistent memory pressure, compare VPS plans on our homepage to find a configuration with adequate memory for your workload.
Conclusion
The vm.* kernel parameters directly control how your VPS handles memory pressure, disk I/O, and process stability. By tuning swappiness, dirty page ratios, overcommit behavior, and cache pressure, you can reduce latency, prevent OOM kills, and make more efficient use of limited RAM. Apply these settings on every new VPS as part of your initial hardening process.


Leave a Reply
You must be logged in to post a comment.