Add A Swap File On Existing Partition

Use case: One creates a cloud VM with minimal RAM and your service is being killed because it just needs some more. There’s only one partition on the disk attached so here we go:


$ sudo fallocate -l 1g /mnt/1GiB.swap
$ sudo chmod 600 /mnt/1GiB.swap
$ sudo mkswap /mnt/1GiB.swap
Setting up swapspace version 1, size = 1048576 kB
$ sudo swapon /mnt/1GiB.swap
$ cat /proc/swaps 
Filename                                Type            Size    Used    Priority
/home/swapfile                          file            1048576 1048576 -1
$ echo '/mnt/4GiB.swap swap swap defaults 0 0' | sudo tee -a /etc/fstab
$ reboot
$ free -h
              total        used        free      shared  buff/cache   available
Mem:            15G        9.3G        454M        4.0G        5.8G        1.9G
Swap:          1.0G        1.0G          0B