How to add Linux Swap file if you don’t have Swap Partition

Picture this, you’re running a lot of process that consumes a lot of memory that even your 2GB RAM is running out faster than you can blink your eye. Suddenly your machine slows down to a crawl, then it begin to hang, and all you can do is pray that you wont lose any valuable data should one of the process be killed.

You realised now that you should have allocate more space to that dwarfish 100MB swap partition, seems like it is too late as you need to repartition your hard disk to resize the swap partition to avoid this problem when running memory intensive program. All is lost…

Enter the Swap file…

Actually you can substitute a swap partition with swap files if you need more swap. What you need to do is create an empty file with the size of the swap that you preferred (for example 500MB) and add this information to your fstab.

How to add more Linux Swap with Swap File

Assuming you want to put it in “/”, Create an empty 500MB file
$ sudo dd if=/dev/zero of=/swap.img bs=1024k count=512

Format it as a swap file
$ sudo mkswap /swap.img

Add it to your running Linux system
$ sudo swapon /swap.img

Optionally you can add /swap.img to fstab for automatic swap activation.

$ sudo gedit /etc/fstab

Add this line at the end of the file
/swap.img none swap sw 0 0

Run “free -m” command to verify that you’ve added the newly created swap to your Linux based operating system.

Hope that helps!

[tags]linux,ubuntu,debian,opensource,open source,fstab,swap[/tags]

13 thoughts on “How to add Linux Swap file if you don’t have Swap Partition

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>