Memory Tuning
Linux kernel memory management parameters allow fine-grained control over how the system allocates, swaps, and caches memory. Tuning these parameters can improve performance for workloads like databases, virtualization, or high-throughput applications. Key parameters include vm.swappiness, kernel.shmall, and others that balance memory usage between application needs and system stability.
vm.swappiness: Controlling Swapping Behavior¶
The vm.swappiness parameter determines how aggressively the kernel will swap memory pages to disk. A higher value (0–100) encourages swapping, while a lower value reduces it.
Default and Behavior¶
- Default:
60(balanced approach) 0: Minimizes swapping (suitable for servers with ample RAM)100: Aggressively swaps (useful for systems with limited RAM)
Example:
# Check current value
sysctl vm.swappiness
# Temporarily set to 10 (reduces swapping)
sudo sysctl -w vm.swappiness=10
# Persistently apply (edit /etc/sysctl.conf)
echo "vm.swappiness=10" | sudo tee -a /etc/sysctl.conf
kernel.shmall and kernel.shmmax: Shared Memory Limits¶
These parameters define the maximum size of shared memory segments (in pages) and the maximum size of a single segment, respectively. They are critical for applications like databases or message queues that rely on shared memory.
Calculation and Defaults¶
kernel.shmall(pages): Default is typically2097152(2GB on 4KB pages)kernel.shmmax(bytes): Default is often134217728(128MB)
Example:
# Check current values
sysctl kernel.shmall
sysctl kernel.shmmax
# Set new values (e.g., 4GB shared memory)
sudo sysctl -w kernel.shmall=524288
sudo sysctl -w kernel.shmmax=4294967296
Note: Ensure kernel.shmmax does not exceed the system’s physical memory (RAM + swap).
vm.vfs_cache_pressure: File System Caching¶
This parameter controls how aggressively the kernel evicts file system cache to free memory. Higher values reduce caching, which may improve responsiveness for memory-intensive tasks.
- Default:
100 - Values > 100: Prioritize application memory over caching
- Values < 10: Prioritize caching
Example:
vm.overcommit_memory and vm.overcommit_ratio: Memory Overcommit¶
These parameters control how the kernel manages memory allocation for processes.
vm.overcommit_memory=0: Heuristics-based overcommit (default)vm.overcommit_memory=1: Always overcommit (useful for applications like databases)vm.overcommit_ratio: Percentage of RAM allowed for overcommit (default:50%)
Example:
# Enable strict overcommit for a database server
sudo sysctl -w vm.overcommit_memory=1
sudo sysctl -w vm.overcommit_ratio=75
vm.min_free_kbytes: Minimum Free Memory¶
This parameter sets the minimum amount of memory (in KB) that must always be free. It prevents the system from using all available memory, which can cause instability.
- Default: ~
16384KB (16MB) for most systems - Adjust based on workload: Higher values for critical services, lower for high-memory systems
Example:
Key takeaways¶
vm.swappinessbalances swapping behavior; set to10for servers.kernel.shmallandkernel.shmmaxdefine shared memory limits; ensure they align with application needs.- Adjust
vm.vfs_cache_pressureto prioritize memory for critical workloads. - Use
vm.overcommit_memoryandvm.overcommit_ratioto optimize memory allocation for specific use cases. - Always test changes in non-production environments and validate with tools like
free,top, andvmstat.