Adjusting Kernel Parameters for Ubuntu Swap
vm.swappinessvm.swappiness controls the kernel’s tendency to move inactive memory pages from RAM to swap space. It ranges from 0 to 100:
To test the impact of vm.swappiness without rebooting:
sysctl (Recommended):sudo sysctl vm.swappiness=<value> (e.g., sudo sysctl vm.swappiness=10). This updates the running kernel configuration immediately.echo <value> | sudo tee /proc/sys/vm/swappiness (e.g., echo 10 | sudo tee /proc/sys/vm/swappiness). This directly modifies the kernel’s runtime parameter interface.For changes to persist after a reboot, edit the system’s kernel parameter configuration:
/etc/sysctl.confsudo nano /etc/sysctl.conf). Add or uncomment the line vm.swappiness=<value> (e.g., vm.swappiness=10). Save the file and run sudo sysctl -p to apply changes without rebooting./etc/sysctl.d/ (Modern Approach)sudo nano /etc/sysctl.d/99-custom-swap.conf) and add vm.swappiness=<value>. Run sudo sysctl --system to load all configurations (including the new file), which overrides /etc/sysctl.conf if there are conflicts.After modification, confirm the new value is applied:
cat /proc/sys/vm/swappiness # Should display the updated value (e.g., 10)
vm.swappiness to 10–30 to balance performance and memory availability.vm.swappiness (10–30) with a swap file on the SSD to reduce latency. For HDDs, keep vm.swappiness at 10–30 to mitigate slow swap I/O.By adjusting vm.swappiness, you can optimize Ubuntu’s swap behavior to match your system’s workload and hardware, ensuring better performance under memory pressure.