A auto swap file configured in a single systemd service.
- Copy the service into:
/etc/systemd/system/swapon.service
- Load the service:
systemctl enable /etc/systemd/system/swapon.service
- Start the service:
systemctl start swapon
Considerations before swapon:
- The file must be in a partition with enough space
- The file should have Copy-On-Write disabled to avoid fragmentation (If the underlying filesystem is btrfs or ZFS).
- The file should have blocks allocated and marked as uninitialized, or should be zeroed. This scripts uses the first approach (Faster than zeroing).
- The file should be protected to being accessed from other users.
- A loop device needs to be associated with the file. This make the file accessible as a block device, and can be mounted as it were a disk device.
Is a kernel parameter that let tweak the way Linux swaps. It's a number from 0 to 100. In essence, higher values lead to more pages being swapped, and lower values lead to more applications being kept in memory, even if they are idle.
If memory is swapped out too quickly then application response time drops, because when the application it's in its CPU quantum, the system has to swap the application back into memory, which will make it feel slow.
The default value for swappiness is 60. You can alter it temporarily (until you next reboot) by typing as root:
echo 50 > /proc/sys/vm/swappiness
If you want to alter it permanently then you need to change the vm.swappiness
parameter in the /etc/sysctl.conf
file.