Linux advanced Swap Management: Swap creation with Virtual file system

Advanced Swap management with virtual file system

Swap partition is a place your system will keep temporary files so that it can do the processing bit faster. Suppose your swap is getting filled and there is no raw partition left for creating swap partition on the server what we can do ?. At any cost your manager asked to increase the swap to improve the system performance.
To come out of this situation there is one solution provided by Linux e.i we can create a swap file with in all ready existing and using partition if that partition is having sufficient free space
Step1:Switch off all the swap before any swap related work
#swapoff -a
Step2: Determine what is the swap size we required(here i am taking 128MB) and execut the following command with count equal to 131072 (because 131072 is equal to 128M) This will create a swap file name swapfile in / directory with size of 128 MB.
#dd if=/dev/zero of=/swapfile bs=1024 count=131072
Let me explain above command.
dd is an excellent command which is used to take backups of partitions, create MBR, create empty files etc. Here dd is used to create a empty file called /swapfile by taking input file as /dev/zero with block size as 1024 byts and number of these bytes as 131072. This command will create a file with 128MB.
Step3: Now set this swap file in order to use this virtual file as swap
#mkswap /swapfile
Step4: Edit the /etc/fstab file to specify the swap file for saving these settings permanently.
#vi /etc/fstab
/swapfile    swap    defaults    0 0
save and exit the file. Learn about vi editor here
Step5: Update the kernel about the mount table changes.
#mount -a
Step6:After all these changes we have to on the swap once again with command as given below.
#swapon -a
Step7: Check weather the swap space is updated or not by using any one of the following command
#free -m   
#swapon -s 
#cat /proc/swaps
Ok we are done with creating the swap file, how about removing it?. Follow below procedure to remove the swap file.

Removing swap

Step1: Before doing any thing with swap first we have to switch off the swap
#swapoff -v /swapfile
Step2: Remove or comment the line of swap file in /etc/fstab file system configuration file
#vi /etc/fstab
 /swapfile    swap    defaults    0 0
then save and exit
Step3: update the kernel about mount table changes
#mount -a

Step4:Remove the swapfile permanently 

#rm /swapfile

Please comment your thoughts on this.

Show your love by sharing this..!
Share on VKShare on StumbleUponShare on YummlyPrint this pageFlattr the authorShare on TumblrEmail this to someoneBuffer this pageShare on Google+Share on LinkedInShare on FacebookTweet about this on TwitterPin on PinterestShare on RedditDigg this
The following two tabs change content below.
My name is Surendra Kumar Anne. I hail from Vijayawada which is capital of south Indian state of Andhra Pradesh. I am a Linux evangelist who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. I work with ICE systems, Sydney as Sr. Consultant. You can contact me at surendra (@) linuxnix dot com.
  • HackSack

    good article :)

    Hope u post more articles

  • Surendra Kumar Chowdary

    thanks for the comment, Please let me know what you want to see more on this blog

  • Surendra Kumar Chowdary

    Hi Dpanupam,

    thanks for your comments please let me know which are other topics you want to see on this blog


    • Thiagu


      Please share the download link, where can i get this e-book full artical.



  • nawaz

    can you show me proper guide to learn linux from basic to advance . I m new in this