How to increase swap size in linux

Swap is a type of filesystem and is a virtual memory. Whenever your RAM is full, your operating system will look for further memory in your swap space. For this reason, you reserve some part of the hard disk to create a swap partition.

 
Identifying Current Swap Space Usage:

root@vishal-desktop:/# cat /proc/swaps
Filename                                Type            Size    Used    Priority
/dev/sda7                               partition       1951740 4       -1


Alternatively, use the swapon command:

root@vishal-desktop:/# swapon -s
Filename                                Type            Size    Used    Priority
/dev/sda7                               partition       1951740 4       -1


Finally, the free command may also be used:

root@vishal-desktop:/# free
             total       used       free     shared    buffers     cached
Mem:        895112     721656     173456          0      36592     310156
-/+ buffers/cache:     374908     520204
Swap:      1952736          4    1952732


Adding a Swap File :

Additional swap may be quickly added to the system by creating a file and assigning it as swap. This is achieved as follows.

The following dd command example creates a swap file with the name swap with a size of 1Gb.

Create the swap file using the dd command :


root@vishal-desktop:/# dd if=/dev/zero of=/swap bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 17.4283 s, 61.6 MB/s

Configure the file as swap:

Change the permission of the swap file so that only root can access it


root@vishal-desktop:/# chmod 600 /root/swap


root@vishal-desktop:/# mkswap /swap
Setting up swapspace version 1, size = 1048572 KiB


Enable the newly created swapfile :

root@vishal-desktop:/#  swapon /swap

Finally, modify the /etc/fstab file to automatically add the new swap at system boot time by adding the following line:

# cat /etc/fstab
/swap  none  swap  sw  0 0

Once the swap space has been activated, verify that it is in use using the swapon –s command:

root@vishal-desktop:/#  swapon -s
Filename                                Type            Size    Used    Priority
/dev/sda7                               partition       1951740 142884  -1
/swap                                   file            1048572 0       -2

root@vishal-desktop:/#  free -k
                     total       used       free     shared    buffers     cached
Mem:        895112     828484      66628          0       2144     539552
-/+ buffers/cache:     286788     608324
Swap:      3000312     142876    2857436

If you don’t want to reboot to verify whether the system takes all the swap space mentioned in the /etc/fstab, use following to enable or disable swap.

root@vishal-desktop:/#  swapoff -a

root@vishal-desktop:/#  swapon -a


De-activate the additional swap space at any time using the swapoff command as follows:

root@vishal-desktop:/# swapoff /newswap


Thanks,
Vishal Vyas

0 comments:

Post a Comment