Temporary Linux swap in user space

Once in a while, you might run into a situation where you need some extra RAM (or swap) for a short time. (I ran into this situation today while stitching some really enormous images in Hugin). Adding a new swap partition isn’t practical if you only need the extra swap space for a short time.

If you have enough space in your home directory (or elsewhere on your filesystem) then you can use this as a temporary swap file without making a partition.

First we allocate the file that will be used as swap. This is where we set the size. In this example, I’ve chosen 10GB.

truncate -s 10G ~/tempswap

Now we tell this file to be a swap file:

mkswap ~/tempswap

And finally we can tell our system to use the new swap file (on top of any existing swap files):

sudo swapon ~/tempswap

To check on your memory and swap availability and usage, try:

free -m

This swap file won’t be used as swap on next boot unless you add it to your /etc/fstab, but you’ll still have to delete the file to get the space back. To clear up after yourself manually, do this:

sudo swapoff ~/tempswap
rm ~/tempswap

2 thoughts on “Temporary Linux swap in user space

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s