Swap Management In Linux
In linux the ram(memory) is divided into small number of blocks called pages. Operating system stores all programs that resides in the memory inside these pages. So pages are used by the operating system to keep data in the RAM. When we run lot of programs the operating system needs more number of pages to keep them. Slowly the operating system will fail to allocate pages to the newly begun program in the RAM because it has already allocated all those available pages to the programs which are already present in the ram. So in a situation like this the OS needs to find out a way to overcome this. Swapping is a solution of this problem. Swapping is the process by which a page of memory is copied to the pre-configured space(It can either be a partition or a file which act as a storage location called swap file) on the hard disk, called swap space, to free that page of memory.
"When we combine the sizes of physical memory and swap space we get the available virtual memory."
Advantage: Swapping is necessary when the system requires more memory than that is physically available, here the kernel swaps out less used pages and gives memory to the current application (process) that needs the memory immediately.
Disadvantage: Compared to memory, disks are very slow. It means the processor needs data to process; it takes the data from the memory. Memory is an extremely faster device. It's speed can be measured in nanoseconds. Disks are much slower devices than memory. It's speed can be measured in milliseconds, so accessing the disk for the data can be tens of thousands times slower than accessing the physical memory. The more the swapping, the slower the system will be because the kernel needs to copy data from memory pages to the disk for the swapping. When the operating system needs those pages back, the kernel will copy back those pages from the disk to memory.
View the swap usage
Here I have 511 MB of swap space and used swap space is zero.
Which type of swap space I am using
We can use either a partition as the swap space or a special file called swap file as the swap space.
We can use either a partition as the swap space or a special file called swap file as the swap space.
"swapon -s" command will show you the details of swap space in detail. As I have shown above my system uses a partition "/dev/sda3" as the swap partition.
Extending the swap space
Step1: Create a partition
I have created a new partition (/dev/sda5) and i am going to add to my existing swap space.
Extending the swap space
Step1: Create a partition
I have created a new partition (/dev/sda5) and i am going to add to my existing swap space.
Step2: Format the partition as swap using "mkswap" command
Step3: Mount the new partition
Set the mount point and file system type as "swap"
Set the mount point and file system type as "swap"
Step4: Activate the newly added swap partition.
To enable swap partition use "swapon -a"
To enable swap partition use "swapon -a"
Now you can see additional swap space in the free -m output.
Remove the newly created Swap Space.
Turnoff the Swap partition that you want to remove.
Turnoff the Swap partition that you want to remove.
Now list the all swap space, /dev/sda5 is inactive now.
Open /etc/fstab and remove mount entry of /dev/sda5 then save it.