SWAP is a space in hard disk that allocated as virtual memory to help physical memory ( RAM ). Your kernel removes the least used memory pages in RAM to the SWAP space when the RAM space is full. SWAP also as memory storage when we hibernate the computer.
There are two types of SWAP, SWAP partition, and SWAP file. When we install the operating system, installers typically create a SWAP partition. Alternatively, we can use the SWAP file method when there is no left unallocated disk.
How to create a SWAP File
First off, make sure there is spare space available in your disk. SWAP require at least the size of your physical memory. For instance, your minimum SWAP size is 8GB if your RAM is 8GB. To check available space in your disk, run df -h
$df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 120G 97G 24G 81% /
So we have 24GB free of space, lets create a 8GB SWAP file.
$sudo fallocate -l 8G /swapfile
If you are using XFS file system, probably fallocate will result an error like swapon failed: Invalid argument. Alternatively, we can use dd command altrough it takes more time to process.
$sudo dd if=/dev/zero of=/swapfile count=8000 bs=1MiB
Then we will get a message like this:
$sudo dd if=/dev/zero of=/swapfile count=8000 bs=1MiB
8000+0 records in
8000+0 records out
8388608000 bytes (8.4 GB, 7.8 GiB) copied, 130.398 s, 64.3 MB/s
After allocating 8GB of space in /swapfile, lets change the permission to 600
$sudo chmod 600 /swapfile
Then let’s convert /swapfile into swap by executing mkswap command
$sudo mkswap /swapfile
DONE! We have created a SWAP file. Let’s activate it. Run swapon
$sudo swapon /swapfile
To verify if our SWAP file is activated, run $sudo swapon -s
$sudo swapon -s
Filename Type Size Used Priority
/swapfile file 8191996 0 -3
Now we have an active SWAP in our laptop, that’s great! However, when we restart our laptop , the SWAP space will be not active and we need to activate it manually. To fix this issue, we need to add a rule in /etc/fstab file for /swapfile.
To add rule in /etc/fstab, open /etc/fstab with your favorite text editor, then append this line
/swapfile swap swap sw 0 0
Alternatively, you can accomplish it by running this command
$sudo echo "/swapfile swap swap sw 0 0" >> /etc/fstab
How to create a SWAP partition
On article above, we had created SWAP space in file mode, but for this time we will create a SWAP space in partition mode. Make sure you have spare unallocated space in your disk.
For this case, I will use my secondary hard disk (/dev/sdb) and check unallocated space with parted program.
$sudo parted /dev/sdb
GNU Parted 3.2
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print free
Model: ATA ST1000LM035-1RK1 (scsi)
Disk /dev/sdb: 1000GB
Sector size (logical/physical): 512B/4096B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
32.3kB 1000GB 1000GB Free Space
(parted)
So we have 100% free space, then we can allocate 8GB for SWAP partition. Let’s create SWAP partition using parted program and follow the dialog/wizard.
$sudo parted /dev/sdb
GNU Parted 3.2
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mkpart
Partition type? primary/extended? primary
File system type? [ext2]? linux-swap
Start? 0.00GB
End? 8.00GB
(parted)
To check our new SWAP partition, run print
(parted)print
Model: ATA ST1000LM035-1RK1 (scsi)
Disk /dev/sdb: 1000GB
Sector size (logical/physical): 512B/4096B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 8000MB 7999MB primary linux-swap(v1) lba
(parted)
Quit parted by typing quit then let’s convert that partition to a SWAP format.
(parted)quit
$sudo mkswap /dev/sdb1
Setting up swapspace version 1, size = 7.5 GiB (7998533632 bytes)
no label, UUID=edfb79bc-33cd-4c4b-bc87-4e7579610e17
Done! We had created a SWAP partition, to activate it, run sudo swapon /dev/sdb1 and to check the result run sudo swapon -s . And again, that SWAP partition will not activated after reboot. We need to add new rule in /etc/fstab
/dev/sdb1 swap swap default 0 0
The Simplest Way to Add SWAP Partition in Linux (GUI)
Now it’s time get the simplest way to add SWAP partition in Linux. This method is preferrable if you are using desktop environment, because we will create SWAP partition in KDE Partition Manager. KDE Partition Manager is a GUI application for KDE desktop environment. If you are using GNOME then GParted is preferrable. No worries, these softwares are work similar.
- First step is open your KDE Partition Manager, then select the hard disk.
- Right click unallocated row, then click new
- Set the parameters like on screenshot below then click OK
- It will show you a new partition in the partition list. In order to write our changes to disk, clik edit > apply. Click confirm operation.
- To activate SWAP partition, right click the partition and click activate SWAP
- However, we still need to manually add /etc/fstab rule to activate SWAP partition every booting.