How to Increase Swap Space Size in Linux System



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.

To check swap status, use:

# swapon –s
     Or
# free –h

Swap partition
A swap partition can be created with most GNU/Linux partitioning tools (e.g. fdisk, cfdisk). Swap partitions are typically designated as type 82, however it is possible to use any partition type as swap.
To set up a Linux swap area, the mkswap command is used. For example:
#mkswap /dev/sda2
 
The mkswap utility generates an UUID for the partition by default, 
use the -U flag in case you want to specify custom UUID:
 
#mkswap -U custom_UUID /dev/sda2
 
To enable the device for paging:
 
# swapon /dev/sda2
 
To enable this swap partition on boot, add an entry to fstab:
/etc/fstab
/dev/sda2 swap swap defaults 0 0

Swap file

As an alternative to creating an entire partition, a swap file offers the ability to vary its size on-the-fly, and is more easily removed altogether. This may be especially desirable if disk space is at a premium (e.g. a modestly-sized SSD).

Swap file creation

As root use fallocate to create a swap file the size of your choosing (M = Megabytes, G = Gigabytes) (dd can also be used but will take longer). For example, creating a 512 MB swap file:
#dd  if=/dev/zero   of=/swapfile  bs=1M   count=512
 
# chmod 600 /swapfile
 
After creating the correctly sized file, format it to swap:
 
#mkswap /swapfile
 
Activate the swap file:
 
# swapon /swapfile
 
Finally, edit fstab to add an entry for the swap file:
 
/etc/fstab
/swapfile swap swap defaults 0 0
 

Remove swap file

To remove a swap file, the current swap file must be turned off.
As root
# swapoff -a
Remove swap file:
# rm -f /swapfile

 
 
 
 
 
 
 




No comments:

Post a Comment