Tuesday, 14 January 2014

How To Manage Swap Space In Linux


Linux partitions its physical RAM (Random access memory) into hurls of memory called pages. Swapping is the methodology whereby a page of memory is replicated to the preconfigured space on the hard drive, called swap space, to free up that page of memory. The joined sizes of the physical memory and the swap space is the measure of virtual memory accessible. Swapping is important for two significant explanations. To start with, when the framework requires more memory than is physically accessible, the part swaps out less utilized pages and offers memory to the present requisition (handle) that needs the memory instantly. 



 

Second, countless pages utilized by a requisition throughout its startup stage might just be utilized for introduction and afterward never utilized again. The framework can swap out those pages and free the memory for different requisitions or actually for the circle store. 

There are two approaches to include a swap space in linux which is demonstrated below.we  can either utilize a devoted hard drive allotment to include new swap space, or make a swap index on an existing filesystem and use it as swap space.



Calcutae swap space is currently used by the system?

Free command displays the swap space. 
free -k shows the output in KB.



 # free -k       total   used    free    shared  buffers cached Mem:    3082356 2043700 1038656 0       50976   1646268 -/+ buffers/cache:      346456  2735900                           Swap:   4192956 0       4192956                     Swapon command with option -s, displays the current swap space in KB.   # swapon -s                               Filename        Type    Size    used    Priority /dev/sda2       partition       4192956 0       -1   Swapon -s, is same as the following.   # cat /proc/swaps                                 Filename        Type    Size    Used    Priority /dev/sda2       partition       4192956 0       -1 



Method 1: Utilize a Hard Drive Partition for Extra Swap Space


When you have an extra hard drive, (or space accessible in an existing disk), make an allotment utilizing fdisk summon. Give us a chance to expect that this part is called /dev/sdc1,if the parcel isn't checked as swap you will adjust it by running fdisk and utilizing the "t" menu option.now setup this recently made segment as swap zone utilizing the mkswap command as appeared below:

# mkswap /dev/sdc1

To make this swap space allotment accessible significantly after the reboot, add the accompanying line to the /etc/fstab record. 


# cat /etc/fstab


/dev/sdc1    swap        swap      defaults      0     0

Confirm if the recently made swap is accessible for your utilization:



 # swapon -s Filename        Type    Size    Used    Priority /dev/sda2       partition       4192956 0       -1 /dev/sdc1       partition       1048568 0       -2   # free -k         total   used    free    shared  buffers cached Mem:    3082356 3022364 59992   0       52056   2646472 -/+ buffers/cache:      323836  2758520                           Swap:   5241524 0       5241524                   



Method 2: Use a File for Extra Swap Space 

In the event that you don't have any extra disk, you can make a record some place on your filesystem, and utilize that document for swap space. The accompanying dd command example makes a swap document with the name "myswapfile" under /root dir with a size of 1024mb (1gb).



 # dd if=/dev/zero of=/root/myswapfile bs=1M count=1024 1024+0 records in 1024+0 records out # ls -l /root/myswapfile  -rw-r--r--    1 root     root     1073741824 Aug 14 23:47 /root/myswapfile 



Now Change the permission of swap file so that only root can access swap file.
# chmod 600 /root/myswapfile

Make file as a swap file by using mkswap command.
# mkswap /root/myswapfile

Setting up swap space version 1, size = 1073737 kB


Enable this newly created swapfile.
# swapon /root/myswapfile

To make this swap file to be used as a swap area even after the reboot, add the following line to the file: /etc/fstab

# cat /etc/fstab
# cat /etc/fstab/root/myswapfileswap   swap   defaults  0 0





Check whether the created swap space is available for  use:



Setup this created partition as swap area by using the mkswap command as given below:



 # mkswap /dev/sdc1   Enable the swap partition: # swapon /dev/sdc1  To make this swap space partition available even after the reboot: /etc/fstab file. # swapon -s               Filename        Type    Size    used    Priority /dev/sda2       partition       4192956 0       -1 /dev/sdc1       partition       1048568 0       -2 # free -k         total   used    free    shared  buffers cached Mem:    3082356 3022364 59992   0       52056   2646472 -/+ buffers/cache:      323836  2758520                           Swap:   5241524 0       5241524                     

 





No comments:

Post a Comment