Swap is a space on a disk that is used as an extension of a computer’s physical memory (RAM). When a system runs out of available RAM, it can temporarily transfer some of the data from RAM to the swap space. This allows the system to continue running, even if all the physical RAM is being used.
There are a few reasons why you might want to enable swap on a Linux system:
- To provide additional virtual memory for the system: If a system has a lot of programs running at the same time, it can run out of physical RAM and start using swap space to store data that would normally be stored in RAM. This can help prevent the system from running out of memory and crashing.
- To allow the system to hibernate: Some systems can be configured to “hibernate” (suspend to disk) when the power button is pressed. In order to do this, the system needs to be able to write the contents of RAM to disk. On systems with little RAM, a swap partition or swap file can be used to store this data.
- To improve system performance: On systems with slow disks, using a swap file on a faster disk (such as an SSD) can improve performance by allowing the system to access data in the swap file more quickly.
Steps to enable swap on Linux using a swap file:
- First, create a swap file with the
fallocate
command. For example, to create a 1 GB swap file, you can use the following command:
sudo fallocate -l 1G /swapfile
- Set the correct permissions on the swap file:
sudo chmod 600 /swapfile
- Mark the file as a swap file:
sudo mkswap /swapfile
- Enable the swap file:
sudo swapon /swapfile
- Make the change permanent by adding the following line to the
/etc/fstab
file:
/swapfile swap swap defaults 0 0
- To verify that the swap file is working, you can run the
free -h
command. You should see the swap file listed under “Swap” in the output.
Note: These steps assume that you are using a system with a systemd
init system. If you are using a different init system, the steps may be slightly different.
How to view and modify swappiness in linux?
The vm.swappiness
parameter controls the kernel’s behavior when it comes to using the swap space on a Linux system. A value of 0 means that the kernel will try to avoid using swap space, while a value of 100 means that the kernel will be aggressive in using swap space.
To view the current value of vm.swappiness
, you can use the following command:
cat /proc/sys/vm/swappiness
To modify the value of vm.swappiness
, you can use the sysctl command. For example, to set vm.swappiness
to 60, you can use the following command:
sudo sysctl vm.swappiness=60
To make the change permanent, you will need to add the following line to the /etc/sysctl.conf
file:
vm.swappiness=60
This will ensure that the vm.swappiness
value is set to 60 every time the system is booted.
The optimal value for vm.swappiness
will depend on your system’s hardware and workload. In general, a value of 60 is a good starting point, but you may want to adjust it based on your system’s needs.
Conclusion
It’s important to keep in mind that using swap can have a negative impact on system performance, as accessing data in swap is slower than accessing data in RAM. Therefore, it’s generally best to have enough physical RAM to meet the needs of the system, rather than relying heavily on swap.