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:
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:
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