Skip to content

Instantly share code, notes, and snippets.

@susanBuck
Created November 15, 2021 20:43
Show Gist options
  • Save susanBuck/b9310f8f0886105008a2f6a18a18d7ae to your computer and use it in GitHub Desktop.
Save susanBuck/b9310f8f0886105008a2f6a18a18d7ae to your computer and use it in GitHub Desktop.
Swap file

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment