How to Optimize Linux Server Swap Usage #
Swap space on Linux servers provides a buffer when physical RAM is fully utilized, allowing the system to use disk space as “virtual memory.” However, excessive swap usage can degrade performance, as disk access is significantly slower than accessing RAM. Optimizing swap usage helps improve overall server responsiveness and stability.
1. Check Current Swap Usage #
To monitor swap usage and determine whether adjustments are necessary, use:
free -h
This command shows the total, used, and available memory and swap space. If swap usage is high while free RAM is available, you may need to adjust swap settings.
2. Adjust the swappiness
Value #
The swappiness
parameter controls how often Linux uses swap. It ranges from 0 to 100:
- Low
swappiness
(0-10): Prioritizes RAM usage and avoids swap. Suitable for high-performance servers where minimizing disk I/O is critical. - Default
swappiness
(60): Suitable for a balanced use of RAM and swap. - High
swappiness
(above 60): Aggressively uses swap. Useful for systems with limited RAM.
To check the current swappiness
value:
cat /proc/sys/vm/swappiness
To temporarily set a new value (e.g., 10
):
sudo sysctl vm.swappiness=10
To make it persistent across reboots, add the following line to /etc/sysctl.conf
:
vm.swappiness=10
3. Monitor and Limit Swapiness with sysctl
#
To dynamically adjust swappiness
based on server conditions, consider using a script that checks memory usage and adjusts swappiness
as needed. This can help optimize swap usage based on current load.
4. Use ZRAM for Compressed Swap in RAM #
ZRAM creates a compressed block device in RAM, reducing the need for disk-based swap and improving performance. It is particularly beneficial on systems with limited physical RAM.
To set up ZRAM: #
- Install
zram-tools
:
sudo apt install zram-tools
This approach compresses swap data in memory, providing faster swap performance compared to traditional disk-based swap.
5. Optimize Swap File Size #
If your server often uses swap, increasing swap size might improve performance. To resize or create a swap file:
- Disable the current swap file:
sudo swapoff -v /swapfile
2. Resize or recreate the swap file (for example, 4GB):
sudo dd if=/dev/zero of=/swapfile bs=1G count=4
3. Set proper permissions:
sudo chmod 600 /swapfile
4. Format the swap file:
sudo mkswap /swapfile
5. Enable the new swap file:
sudo swapon /swapfile
6. Make it persistent by adding to /etc/fstab
:
/swapfile swap swap defaults 0 0
6. Monitor Swap with vmstat
and top
#
Tools like vmstat
and top
allow you to observe swap activity and evaluate swap performance:
vmstat
: View system performance, including swap in/out activity
vmstat 5
top
: Observe swap usage per process and sort by memory usage.
top
Monitoring helps identify high-memory processes causing excessive swap usage.
7. Use Swap Partition Instead of Swap File (Optional) #
For servers under heavy load, using a dedicated swap partition may offer better performance compared to a swap file. To create a swap partition:
- Use a partitioning tool like
fdisk
orparted
to create a new partition. - Format it as swap:
sudo mkswap /dev/sdX2
3. Enable the partition:
sudo swapon /dev/sdX2
4. Add to /etc/fstab
:
/dev/sdX2 swap swap defaults 0 0
8. Tune Cache Pressure (vm.vfs_cache_pressure
) #
The vfs_cache_pressure
setting controls the tendency of the kernel to reclaim memory used for caching directory and inode objects. Lowering it allows the system to retain more cached data.
To check the current value:
cat /proc/sys/vm/vfs_cache_pressure
To temporarily set it (e.g., to 50
):
sudo sysctl vm.vfs_cache_pressure=50
To make this change permanent, add to /etc/sysctl.conf
:
vm.vfs_cache_pressure=50
9. Disable or Limit Swap for Specific Processes #
Using cgroups (control groups), you can restrict swap usage for particular processes. This is useful if certain processes should avoid swap for performance reasons.
- Install
cgroup-tools
:
To temporarily set it (e.g., to 50
):
sudo sysctl vm.vfs_cache_pressure=50
To make this change permanent, add to /etc/sysctl.conf
:
vm.vfs_cache_pressure=50
9. Disable or Limit Swap for Specific Processes #
Using cgroups (control groups), you can restrict swap usage for particular processes. This is useful if certain processes should avoid swap for performance reasons.
- Install
cgroup-tools
:
sudo apt install cgroup-tools
2. Create a control group with limited memory:
sudo cgcreate -g memory:/no-swap-group
sudo cgset -r memory.swappiness=0 no-swap-group
3. Assign a proce
sudo apt install cgroup-tools
2. Create a control group with limited memory:
sudo cgcreate -g memory:/no-swap-group
sudo cgset -r memory.swappiness=0 no-swap-group
3. Assign a process to this group:
sudo cgexec -g memory:no-swap-group your_command
Author’s final word #
Optimizing swap usage on a Linux server involves balancing between memory, disk space, and system responsiveness. By carefully tuning parameters like swappiness
, using tools like ZRAM, resizing swap space, and monitoring server performance, you can significantly improve your server’s responsiveness and make better use of available resources.