Before doing any thing about swap we should know what is swap, for what purpose the swap is useful and other stuff related to swap management.

What is a swap in Linux?

When we are running applications in Linux their frequently used data is stored in RAM for frequent access. If the applications uses the full RAM there is a possibility of system slowness. The left solution is to increase RAM size so that we can run those applications without any issue. As RAM is bit costly, buying it is not preferred plan A option. So people come with a solution where your own hard-disk can be used as RAM(in fact virtual RAM or called as swap) it self so that RAM data which is not frequently accessed can swapped to HDD swap location for feature access.

About memory hierarchy in Linux

When your machine is processing data it uses some memory hierarchy to swap the memory for effective processing the data and applications. Below is the memory hierarchy up to Swap this swapping take place.

CPU registers(hundred of bytes)
L1 cache(in KB's)
L2 cache(in MB's)
L3 cache(in 100's of MB's)
RAM (in GB's)
Swap(1.5 times of RAM)
HDD(1000's of GB's)
SAN/NAS(in TB's)
Tape Drives(100's TB's)

Q.Why we require a swap file, without swap can a system exit’s?
Swap is required for proper working of machine if the RAM size is less. With out Swap we can run a machine if we have enough RAM.

Q. Why is swap size is less than or equal to 1.5 times the RAM?
This is kind of thumb rule. If we add up memory from CPU registers to till RAM that will accounts to 1.5 times the RAM. In other words if your processes swaps entire this memory to Swap partitions it’s easy to take that running applications to their memory locations for proper running of applications.

Steps to crate Swap in Linux

Step 1: First check what is the swap space the system is having and utilization of swap size. To view swap details in Linux execute below commands.

#free -m
#swapon -s
#cat /proc/swaps

-s in swapon command stands for stats

-m in free command stands for Mb


root@linuxnix:/proc# free -m
 total used free shared buffers cached
Mem: 7759 7284 474 535 17 740
-/+ buffers/cache: 6527 1231
Swap: 15258 4600 10658
root@linuxnix:/proc# swapon -s
Filename Type Size Used Priority
/dev/sda3 partition 15625212 4710908 -1
root@linuxnix:/proc# cat /proc/swaps 
Filename Type Size Used Priority
/dev/sda3 partition 15625212 4710872 -1

“free -m” will give output of swap statistics in Megabytes(m), if we want the output in Kilobytes then free -k

Step 2: Before starting the Swap creation/modifications we have to take precautions such as switch off all the swap and no user should be logged in

#swapoff -a

Step 3: Check in the system if there is any raw space in the system

#fdisk -l

Step 4: If the system is having free space then create a partition which support swap(partition type 82) with required amount of free space

#fdisk /dev/hda
p                #press p to print the partition table
n                #press n for creating new partition
256M          #specify the amount of swap required
t                 #press t to change the partition type to 82 (because partition type 82 is well supported for swap)
8                 #enter the partition no on which u want to create swap(here i am creating on /dev/hda8 partition)
82               #specifying the partition type
p                 #press p to print the partition table and to just conform the /dev/hda8 partition type
w                #press w for writing the changes to partition table (if suppose if u have any problem or trouble just                                           press q to quit from fdisk utility without any problem)

Step 5: update the partition table changes to kernel so that there is no need to restart the system/server


Step 6: Permanently mounting the partition details,in order to do this one we have to update the /etc/fstab file

#vi /etc/fstab

Content into file:

/dev/hda8    swap    swap    defaults    0 0

enter the above line in to /etc/fstab file,save it and exit from editing /etc/fstab file

Step 7: Formatting/creating swap signature on the newly created partition

#mkswap /dev/hda8

Note: stpe 6 and step 7 are interchangeable.

Step 8: Update the mount table to kernel

#mount -a

Step 9: Now on the swap so that it can be available for use.

#swapon -a

Step 10: Check weather the swap is updated or not

#free -m
#swapon -s
#cat /proc/swaps

Removing swap in Linux

Step 1: Before doing any thing with swap first we have to switch of the swap

#swapoff -a

Step 2: Remove/comment the entry of swap from /etc/fstab file

#vi /etc/fstab

then save and exit

Step 3: Update the kernel about mount table changes

#mount -a

Step 4: Remove the partition used by swap

#fdisk /dev/hda8
d                #press d to delete the partition
8                #specify the partition no to be deleted
w                #press w to write the changes to partition table and quit the fdisk utility

Step 5: update the partition table changes to kernel without rebooting the system/server


Step 6: Now on the swap

#swapon -a

Step 7: Now check weather swap is updated properly or not

#free -m
#swapon -s
#cat /proc/swaps
The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.