Dynamic and static swap file

I prefer to have my swap in a file instead of a partition because it’s more flexible, and because 99% of time my swap is not used intensively. If it happen to be used intensively it means that there is something buggy in my applications.

I used the swapspace and dphys-swapfile Debian package for several years. Although it work pretty well most of the time, it happen that when I need a lot of swap in a short time (i.e. allocation of 1GB in 1s), the swap doesn’t extend fast enough and I run out of memory. That’s why on computers where I need to do such things I switched to a static swap file:

dd if=/dev/zero of=/var/lib/swap.img bs=1M count=1024
chmod 0600 /var/lib/swap.img
mkswap /var/lib/swap.img
echo '/var/lib/swap.img none swap sw 0 0' >> /etc/fstab
swapon -a