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]
Keep updated with the latest posts, be a part of over 1,000 subscribers! :
Subscribe to your email
You might also want to read...
- Mypapit’s blog posts featured in US Patent #8244955
- List partition types in Linux with fdisk
- iotop: How to monitor processes that hogs hard disk writes
- Mount Windows Partition in Ubuntu
- My Experimental PC Harddisk Layout
- Read and Write NTFS filesystem with Ubuntu Linux
- When Microsoft write about GNU/Linux-related tutorial


November 30th, -0001 at 12:00 am
here
July 25th, 2007 at 1:47 pm
[...] YouTube Link to Article open source How to add Linux Swap file if you don’t have Swap Partition » Posted at mypapit gnu/linux blog on Wednesday, July 25, 2007 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 … , opensource, open source, fstab, swap View Entire Article » [...]
July 27th, 2007 at 11:54 pm
thanks, very nice article.. im on linux too, a linux girl. lol
July 28th, 2007 at 2:19 am
thanks for the tip . lame me for using partition magic to resize or create my swap space. never knew this tips before . thehehe
July 30th, 2007 at 4:17 pm
[...] Your page is on StumbleUpon [...]
August 2nd, 2007 at 5:26 pm
[...] How to add Linux Swap file if you don’t have Swap Partition [...]
August 27th, 2007 at 1:51 am
[...] Re: Necessary for Swap partition? Yes, you can use a swap file instead of a swap partition: http://blog.mypapit.net/2007/07/how-…partition.html Here’s how __________________ ____________________________ .:. Arch Linux .:. [...]
April 10th, 2008 at 6:01 pm
Coooool tip to avoid resizing of the linux
partitions without re-partitioning / other
utilities.
Karthik Balaguru
September 9th, 2008 at 1:37 am
Thank you, this is really helpful!
I just wonder if I can put the swap file on other device (like /dev/sda1) instead of root folder /
October 16th, 2008 at 5:58 am
To Hoang:
I don’t know if you’ve found the answer to this already or not, but yes, you can put the swap file anywhere you want it. You can also name it whatever you like, I’m pretty sure. As long as your running Linux system knows where to find it, you should be good.
November 10th, 2008 at 9:07 am
Great article!
Only a question. What if I want to make a partition of a different size? For example, a 250 MB one. What should I change in the following line?
$ sudo dd if=/dev/zero of=/swap.img bs=1024k count=512
Should I change bs=1024k into bs=512k and count=512 into count=256 OR only count=512 into count=256 ?
Thanks in advance.
December 22nd, 2008 at 5:57 am
@George:
It doesn’t matter. I would change count=512 into count=256.
December 22nd, 2008 at 6:32 am
thaks a milion !