Setting Up and Resizing the Swap Space
Swap is a virtual memory mechanism that moves certain parts of memory, usually the ones not in use, from RAM to secondary storage, freeing up RAM for active tasks.
Viewing current swap information
Before performing any operations on swap, we recommend checking its current type and location with the following command:
swapon -s
The output of this command may look like this:
Filename Type Size Used Priority
If the output is "empty", there is no swap on the system. To create swap, follow these instructions.
The output may also contain lines similar to these:
Filename Type Size Used Priority
/dev/xvda5 partition 1046524 4568 -1
This means the xvda5 partition is used for swap. If you plan to increase the amount of swap, you can create a swap file that will be added to the existing swap space.
Another possible output:
Filename Type Size Used Priority
/swap file 1048572 0 -1
This means the /swap file is used for swap. If you want to resize it, use these instructions (skip step 6, since the swap entry already exists in the /etc/fstab file).
Creating and resizing swap
To resize existing swap or create a swap file, follow these steps:
1. First, turn off the current swap:
swapoff -a
2. Then resize the swap file. If you don't have one yet, this command will create it:
sudo dd if=/dev/zero of=/swap bs=1M count=1024
Replace count with the swap size you need, for example 512, 1024, or 4096.
3. Prepare the swap file you created:
chmod 600 /swap && mkswap /swap
4. Turn on the new swap:
swapon /swap
5. Check the result with the swapon -s command:
swapon -s
Filename Type Size Used Priority
/swap file 1048572 0 -1
6. To keep these changes after a server reboot, add the swap entry to the /etc/fstab file:
echo "/swap swap swap defaults 0 0"| sudo tee -a /etc/fstab