Sunday, December 15, 2013

Linux Swap Size and How to free increased swap space

What is Swapping?

In modern Operating Systems, program which is accessed less frequently can be temporarily stored on disk or other media, So that this space can be used by some other program which really need it.This is called "swapping",  as an area of memory that can be used by others and what this contains can be swapped or exchanged easily. Swapping is also known as "Paging".

Features of Swapping:

   Mainly, Swapping has two important features:
    1. Useful for executing the programs which need memory more than available physical memory. In these scenarios, Kernel/OS move out less used programs from main memory and move in the process that needs memory immediately to memory.
    2. Any, application which loads number of pages during its startup and then don't use those ones any more. Now such unused pages can be stored in swap space, so that main memory can be freed for other programs.

Swap Space in Linux-

        In Linux, normally during installation we create swap partition(used for swapping purpose). Mostly, we create swap partition/file of size equal to 2*sizeof(RAM). This is because we need  almost double space while doing swapping for any program(hope you already know this, not going in details). 

Note : On system where we have enough RAM present to handle memory requirements for all processes, we don't need to create swap partition/file twice of RAM. As, this will not be fully utilized and will be wastage of space.

How to free/clear the Swap Partition/File when gets filled up?

Now, let us come to main topic. Suppose any time due to number of swap in/out your all space gets utilized and you want to free up swap partition/file. In that case you can follow below steps.

        You may be wondering that I am using two terms partition and file. So, Partition is logical portion of our HDD and file is the special file that we have created on existing partition that can be used for swapping .

To check what swap space you have. you can use "swapon -s" as root.
# swapon -s
Filename                                Type            Size    Used    Priority
/dev/sda3                               partition       4096564 308     -1

Here, Type field indicate that we have swap space as "Partition" rather than file and logical partition is "/dev/sda3". Size column show the size in "KB", Used column shows How many space has been used in "KB" and finally Priority columns tell kernel to which swap space is to be used first.

Currently, I have 308 KB of swap space is used and I want to free up all the used space. But, before clearing you used swap space, make sure you have enough free main memory to hold the pages which currently reside in space space. In, my case I have enough free memory to hold pages in swap space. So, I am moving ahead with this :).

#free -o
                    total           used              free        shared     buffers     cached
Mem:      49453924   49002192     451732          0       1547144   24390976
Swap:      4096564        308          4096256

# swapoff -a && swapon -a
          # free -o
                            total          used               free        shared    buffers     cached
         Mem:      49453924   49002572     451352          0    1547816   24391608
         Swap:      4096564          0             4096564

Note : Time taken to free up your swap space depends on the size of your swap partition/file. More is the size, more will be the time taken to free up space.

Hope this will help you!!

Thanks! !