Swap space in a extension of RAM memory, to hard disk. When RAM becomes full the inactive pages will be moved to swap space creating room for new active pages. Allocating Swap space is vital for smooth running of Linux machines.
Linux provides three options for creating swap space.
1. Create swap file
2. Create swap partition
3. Extending swap on LVM2 Logical Volume
1. Creating swap file
#Create a blank file with pre allocated size to 2000*1KB = 2000KB.
dd if=/dev/zero of=/var/local/swapfile bs=1k count=2000
mkswap -f /var/local/swapfile
swapon -a /var/local/swapfile
2. Create swap partition
#Say /dev/sda is partition
mkswap /dev/sda
swapon -a /dev/sda
3. Extending swap on LVM2 Logical Volume. e.g, (/dev/VolGroup00/LogVol01 is the volume need to be extended by 2 GB)
#Disable swap for that logical volume:
swapoff -v /dev/VolGroup00/LogVol01
#Resize the LVM2 logical volume
lvresize /dev/VolGroup00/LogVol01 -L +2G
#Format the new swap space
mkswap /dev/VolGroup00/LogVol01
#Enable the extended logical volume partition
swapon -v /dev/VolGroup00/LogVol01
Note: Swap space can be created by combining both Volume and File methods.
#To make the swap to be permanent during every boot, put entry in fstab. Sample is given below.
/dev/VolGroup00/LogVol01 swap swap defaults 0 0
Disabling swap
Inorder to add/reduce/remove space from swap space first that partition/file that need to be disabled.
#Disable swapping on logical volume partition
swapoff -v /dev/VolGroup00/LogVol01
#Disable the swap file (where /var/local/swapfile is the swap file)
swapoff -v /swapfile
#Disable all swap space
swapoff -a
great info suresh..Please clarify me the difference between swap and paging?
ReplyDeleteSwap file or page file both are same.
ReplyDeleteWhenever the physical memory (RAM) become full, the older inactive set of blocks will be moved temporarily to hard disk space which is nothing but swap space. This is called swap out.These data will be brought back to physical memory when they are needed. This process is called swap in.
The unit of data which is moved during either swap in or swap out is called page. In redhat the page value is defaulted to 4096 or 4KB. This value can be changed by changing kernel parameter PAGE_SIZE.
#getconf PAGE_SIZE
4096