In this note set we’ll configure our low-memory DigitalOcean droplets to use a swap file, which will help optimize it for memory-intensive tasks.
"Swap is an area on a hard drive that has been designated as a place where the operating system can temporarily store data that it can no longer hold in RAM." -ref
To configure a swap file, run through the following commands.
First, create the swap file:
$ sudo fallocate -l 4G /swapfile
Next, adjust permissions on the resulting swap file so it isn't readable by anyone besides root:
$ sudo chmod 600 /swapfile
Next, tell the system to set up the swap space:
$ sudo mkswap /swapfile
Next, enable the swap space:
$ sudo swapon /swapfile
Finally, we want to make it so the server always enables this swap space, even after a reboot. To do this, open /etc/fstab with nano:
$ sudo nano /etc/fstab
...and paste in the following line at the end:
/swapfile none swap sw 0 0
Save your changes (ctrl
+ x
, then y
, then Enter).
Confirm it worked: You can confirm your swap file with the following command:
$ sudo swapon -s
Expected output:
Filename Type Size Used Priority
/swapfile file 4194300 0 -2