add virtual memory for one-off task

Mar 23, 2016   #linux  #workarounds  #memory 

Has it ever happened to you that you are lacking “just 4 more gigs of RAM” for running a memory-hungry process? You can add more swapspace without rebooting or changing volume size.

fallocate -l 4G /tmp/swap.tmp
mkswap /tmp/swap.tmp
sudo swapon /tmp/swap.tmp

and then remove when it is no longer needed:

sudo swapoff /tmp/swap.tmp
rm /tmp/swap.tmp

Using dd is a more classical way of allocating a file, but fallocate is just way, way faster (on my laptop it was 2 minutes versus 0.006 seconds).