Atlantic.Net Blog

How to Manage a Swap Partition in Linux

Swap is a physical space on the disk that is used when the system RAM is full. When the memory usage in a system exceeds the available RAM, the kernel will move the idle page to the swap memory. Swap space can be created on a separate partition or a swap file. If your server is running on a VPS and a swap partition is not present, then you will need to create a swap file.

In this post, we will show how to create and manage a swap space on Linux.

Step 1 – Verify Swap Partition

Before starting, you will need to check whether Swap is enabled or not in your system.

You can check it with the following command:

swapon --show

If the output is empty that means there is not any swap space active in your system.

Step 2 – Create a Swap File

As you can see, there is not any swap space active in your system. So you will need to create a new swap file to your system.

First, create a swap file with size 4GB using the following command:

dd if=/dev/zero of=/swapfile bs=4096 count=1048576

You should see the following output:

1048576+0 records in
1048576+0 records out
4294967296 bytes (4.3 GB, 4.0 GiB) copied, 9.47796 s, 453 MB/s

Next, set the correct permission on swapfile with the following command:

chmod 600 /swapfile

Next, create a swap area on the swapfile with the following command:

mkswap /swapfile

Output:

Setting up swapspace version 1, size = 4 GiB (4294963200 bytes)
no label, UUID=035ada64-2c1a-407d-9f1a-c0dd02b8dcd4

Next, activate the swap space using the following command:

swapon /swapfile

The above command will activate the swap space temporarily.

To set up it permanently, edit the /etc/fstab file:

nano /etc/fstab

Add the following line:

/swapfile swap swap defaults 0 0
Save and close the file then verify the swap partition with the following command:
swapon --show

You should see the following output:

NAME      TYPE SIZE USED PRIO
/swapfile file   4G   0B   -2

Step 3 – Check Swap Usage

To check the Swap usage information, run the following command:

free -m

You should see the following output:

              total        used        free      shared  buff/cache   available
Mem:           1987          74          69           0        1843        1745
Swap:          4095           0        4095

You can also check it with the following command:

cat /proc/swaps

You should see the following output:

Filename                                Type            Size    Used    Priority
/swapfile                               file            4194300 0       -2

You can also use the top command to check the swap usage in real-time:

top

You should see the following output:

Tasks:  84 total,   1 running,  83 sleeping,   0 stopped,   0 zombie
%Cpu(s):  5.9 us,  5.9 sy,  0.0 ni, 88.2 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :   1987.7 total,     68.0 free,     74.8 used,   1844.9 buff/cache
MiB Swap:   4096.0 total,   4096.0 free,      0.0 used.   1745.5 avail Mem 

You can also use the vmstat command to check the swap usage:

vmstat 2 6

You should see the following output:

procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 3  0      0  69356  12096 1877228    0    0    35   652   31   59  0  0 99  0  0
 0  0      0  69420  12096 1877228    0    0     0     0   24   42  0  0 100  0  0
 0  0      0  69388  12096 1877228    0    0     0     0   22   40  0  0 100  0  0

Step 4 – Remove a Swap Space

In order to remove the swap space, you will need to deactivate the swap space first. You can do it with the following command:

swapoff -v /swapfile

Next, edit the /etc/fstab file and remove the following line:

nano /etc/fstab

Remove the following line:

/swapfile swap swap defaults 0 0

Next, remove the swap file using the following command:

rm -rf /swapfile

Next, verify whether the swap space is removed or not with the following command:

swapon --show

Conclusion

In the above guide, you learned how to check and create a swap space on Linux. You also learned how to monitor the swap space with different commands. Get started today with your VPS from Atlantic.Net.

Get a $250 Credit and Access to Our Free Tier!

Free Tier includes:
G3.2GB Cloud VPS a Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year