Richbits

Make a swap file

I have 8GB of RAM. Ergo, I should never need swap. I want that space available for my stuff, not locked uselessly in a swap partition I'll never use.

At least, that's what I think until I start loading up some massive data sets, all the processors start whirring, and the computer begins thrashing as it searches for that elusive 10MB of additional RAM it wishes it had. In such a case, a little temporary swap can be quite handy.

Fortunately, this is Linux, and anything is possible.

Let's create an empty file of a predefined size using either:

sudo fallocate -l 512M ./swapfile

or

sudo dd if=/dev/zero of=./swapfile bs=1M count=512

Give it the correct permissions (we don't want the file to be readable by anyone but root, because danger):

sudo chmod 600 ./swapfile

Format the file as swap:

sudo mkswap ./swapfile

Activate the swap file:

sudo swapon ./swapfile

Later, you can deactivate the swap file with:

sudo swapoff ./swapfile

And then you can eliminate it:

sudo rm ./swapfile