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.
Swap space can be a dedicated swap partition (recommended), a swap file, or a combination of swap partitions and swap files.
According to RedHat, the size of swap space can be calculated by using the following formulae.
M = Amount of RAM in GB, and S = Amount of swap in GB, then
If M < 2
S = M *2
Else
S = M + 2
Basically you can create virtual memory by two ways...
1. Create a new partition
2. Use existing partition
Create a new partition
First of all create a new partition.It should be double of RAM in size
Change its ID to 82. 82 is the ID of swap
Now run the following command
[root@localhost ~]# mkswap /dev/sda7 <-------i m assuming the you have created sda7 partition
Note: mkswap format this partition into swap file system
[root@localhost ~]# swapon /dev/sda7
Note: swapon command will activate the swap file system >Now edit your fstab
[root@localhost ~]# vim /etc/fstab
/dev/sda7 swap swap defaults 0 0
save and exit the file and mount it
[root@localhost ~]# mount -a
you can check the status of your swap by the following command
[root@localhost ~]# swapon -s
Use existing partition
Suppose i have a ram of 256 mb and also have a partition /dev/sda6 which has free space space upto 512mb,then i will use this partition for create virtual memory
[root@localhost ~]# dd if = /dev/zero of = swapfile bs = 512 mb count=1
dd -->disk to disk copy,,
if --> input file,,
/dev/zero -->accumlate zero,,
of --> output file,,
swapfile --> it can be any name,,
bs --> block size,,
512 mb --> size of swap ,,
count=1 --number of times command runs,,
>Now run the following commands
[root@localhost ~]# mkswap swapfile
[root@localhost ~]# swapon swapfile
[root@localhost ~]# vim /etc/fstab
/swapfile swap swap defaults 0 0
save and exit the file and mount it
[root@localhost ~]# mount -a
If you wanna unactivate the swap, run following command
[root@localhost ~]# swapoff swapfile