Swap space in Linux is used when the amount of physical memory (RAM) is full. If the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space. While swap space can help machines with a small amount of RAM, it should not be considered a replacement for more RAM. Swap space is located on hard drives, which have a slower access time than physical memory.
You can either use a dedicated harddisk partition to add new swap space, or create a swap file on an existing filesystem and use it as swap space.
Show Swap Size
Note : free command displays RAM and Swap space of system.
# free
Note : swapon -s command displays partion of swap space.
# swapon -s
Method 1 : Add New Harddisk
If you have an additional hard disk or space available in an existing disk, you can create a partition using fdisk command. For example, this disk partion is /dev/sde1
Now setup this newly created partition as swap area using the mkswap command as shown below.
# mkswap /dev/sde1
Note : Enable the swap partition for usage using swapon command as shown below.
# swapon /dev/sde1
Note : To make this swap space partition available even after the reboot, add the following line to the /etc/fstab file.
# vi /etc/fstab
/dev/sde1 swap swap defaults 0 0Note : After all of that, you can see swap size with swapon -s command.
Method 2 : Using A File For Swap Space
If you don’t have any additional disks, you can create a file somewhere on your filesystem, and use that file for swap space.
Note : The following dd command example creates a swap file with the name “mynewswap” under /root directory with a size of 1024MB (1GB).
# dd if=/dev/zero of=/root/mynewswap bs=1M count=1024
1024+0 records in
1024+0 records outNote : Change the permission of the swap file so that only root can access it.
# chmod 600 /root/mynewswap
Note : Make this file as a swap file using mkswap command.
# mkswap /root/mynewswap
Setting up swapspace version 1, size = 1073737 kBNote : Enable the newly created swapfile.
# swapon /root/myswapfile
Note : To make this swap file available as a swap area even after the reboot, add the following line to the /etc/fstab file.
# cat /etc/fstab
/root/mynewswap swap swap defaults 0 0