Adding SWAP size - Ubuntu/Debian
One of the simplest ways to enhance your application's memory protection is by adding Swap memory. Swap is an area on your hard drive used for temporarily storing operating system data when the required data doesn't fit into the RAM.
By configuring Swap memory, you increase the amount of information your server can retain. Disk space is primarily used when there's a shortage of RAM.
Data written to the disk is slower than data stored in RAM, but the operating system uses Swap for older data.
It's not advisable to use Swap with SSDs, as it can lead to hardware degradation issues.
Checking Swap
You can have multiple Swap files or Swap partitions, but typically, one should suffice.
Check if the system has Swap configured using:
sudo swapon -s
Another way to verify Swap is by using the "free" tool, which shows system memory usage:
free -m
Checking Available Disk Space
The traditional way to allocate space for Swap is by creating a separate partition. However, modifying the partition structure isn't always possible.
You can easily create a Swap file on an existing partition. Before creating a Swap file, check the available disk space:
df -h
The initial value for Swap is either equal to or double the amount of RAM on your system.
Creating a Swap File
Once you know the available disk space, you can proceed to create a Swap file.
Create a file and name it "swapfile" in your root (/) directory. The file must be allocated with the amount of space you want for your Swap file; in this case, it's 4GB.
There are two methods to achieve this:
Slower Method
Traditionally, you create a file with allocated space using the "dd" command. Set the file size by combining the "bs" parameter for block size and "count" for the number of blocks.
For a 4GB file, you can set the block size to 1GB and the count to 4:
sudo dd if=/dev/zero of=/swapfile bs=1G count=4
Double-check the command before confirming with the Enter key to avoid setting the output file to the wrong location.
Confirm that 4GB has been allocated by using:
ls -lh /swapfile
-rw-r--r-- 1 root root 4.0G /swapfile
This command will take some time to complete as it writes 4GB of zeros to the disk. If you want to create the file faster, you can delete the one you just created:
sudo rm /swapfile
Faster Method
A quicker way to create the same file is by using the "fallocate" program. You can create a 4GB file with:
sudo fallocate -l 4G /swapfile
Check the file size with:
ls -lh /swapfile
Enabling the Swap File
Before enabling it, you need to adjust the permissions. Allowing regular users to read and write to the swapfile would be a security risk, so change the permissions to make it readable only by the root user.
sudo chmod 600 /swapfile
Set up the Swap space using:
sudo mkswap /swapfile
Enable the Swap file with:
sudo swapon /swapfile
Verify that Swap is active with:
sudo swapon -s
You should see the newly created /swapfile in the output.
Permanent Swap File Enablement
Now, your Swap file is running. However, when you restart your server, the Swap file won't be enabled automatically. To make it persistent, you need to edit the "fstab" file.
Edit the file with root privileges in a text editor:
sudo nano /etc/fstab
Add a line at the end of the file, instructing the system to automatically enable the file you created:
/etc/fstab
/swapfile none swap sw 0 0
Save and close the file. When you restart the device, the Swap file will be active.
Adjusting Swap Settings
There are a few options you can set that will impact your system's performance when it's swapping.
The "swappiness" parameter determines when the system will move data from RAM to Swap. The value for this parameter is a percentage between 0 and 100.
With values closer to 0, the kernel won't move data to Swap until it's necessary. With values closer to 100, the kernel will prioritize saving data to Swap over RAM.
You can check the current swappiness value with:
cat /proc/sys/vm/swappiness
For VPS, it's better to have a swappiness value closer to 0. For a quick change, you can use:
sudo sysctl vm.swappiness=10
This change will be lost on reboot. To keep the change after a reboot, you can use:
sudo nano /etc/sysctl.conf
Add the line to the end of the file:
/etc/sysctl.conf
vm.swappiness=10
Save and close the file.
Done.