How to Add Swap Space on Ubuntu 26.10
This guide is applicable only for KVM-based VPSes and dedicated servers. If you wish to increase swap on your OpenVZ-based VPS, please contact us via a Support ticket from the client area.
Swap space, also known as "swap," is a reserved area on a server's storage device that the operating system can use as virtual memory when allocated memory is fully utilized. When your server's memory is exhausted due to running applications and processes, the operating system moves less frequently used data from memory to the swap space. This frees up memory for more active tasks and helps prevent the system from slowing down or crashing due to memory exhaustion.
Check Swap Information
To check the swap size:
swapon --show
Output:
root@vps:~# swapon --show
NAME TYPE SIZE USED PRIO
/dev/vda2 partition 512M 0B -1
Or you can check using the free command:
free -m
Output:
root@vps:~# free -m
total used free shared buff/cache available
Mem: 7423 659 5690 4 1426 6764
Note: If the output is empty, it means that swap space is not enabled yet on your system.
Add Swap Space
To create a swap file:
Note: This example adds 1GB. You can change
1Gto your preferred size.
fallocate -l 1G /swapfile
Or use the following command:
dd if=/dev/zero of=/swapfile bs=1024 count=1048576
Set the correct permissions:
chmod 600 /swapfile
Set up a swap area on the file:
mkswap /swapfile
Output:
root@vps:~# mkswap /swapfile
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=97c7c23a-d4e1-4c44-a1d2-5e67e96c3d7f
Activate the swap file:
swapon /swapfile
Make the swap permanent by adding it to the /etc/fstab file:
nano /etc/fstab
Add this line at the end of the file:
/swapfile swap swap defaults 0 0
Check the new swap space:
swapon --show
Output:
root@vps:~# swapon --show
NAME TYPE SIZE USED PRIO
/dev/vda2 partition 512M 0B -1
/swapfile file 1024M 0B -1
Or:
free -m
Output:
root@server:~# swapon --show
NAME TYPE SIZE USED PRIO
/swap.img file 2.2G 0B -2
/swapfile file 1024M 0B -3
Or:
root@vps:~# free -m
total used free shared buff/cache available
Mem: 7423 657 5690 4 1427 6765
Swap: 1535 0 1535
Note: You can now see the added swap space.
Remove Swap Space
Deactivate the swap space:
swapoff -v /swapfile
Output:
root@vps:~# swapoff -v /swapfile
swapoff /swapfile
Edit /etc/fstab to remove the swap entry:
nano /etc/fstab
Remove the following line:
/swapfile swap swap defaults 0 0
Delete the swap file:
rm /swapfile
If you cannot configure swap or face technical difficulties, kindly reach out to us via a Support Ticket.
CrownCloud - Get a SSD powered KVM VPS at $4.5/month!
Use the code WELCOME for 10% off!
1 GB RAM / 25 GB SSD / 1 CPU Core / 1 TB Bandwidth per month
Available Locations: LAX | MIA | ATL | FRA | AMS