Question

Swap partion creation and priority

Hi guys,

I’m looking to create a swap partition on my server. I want it to be at least 3 GB big so that I can actually have more RAM there and server my websites faster or is it not how that works?

Additionally, if possible I would like to have priority of partition and if I see that it works good, I’ll add more.

Can you please let me know how to approach this?


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

KFSys
Site Moderator
Site Moderator badge
August 12, 2019
Accepted Answer

Hi Remdore,

I see what you are trying to do, however SWAP is not as good as actual RAM. You might ask why? Simply put, virtual memory is a combination of RAM and disk space that running processes can use. Swap space is the portion of virtual memory that is on the hard disk, used when RAM is full. However SWAP is actually HDD/SSD so it’s slower. If the applications on your droplet do actually need to use that much SWAP they’ll be considerable slower. Having said that it’s good to have SWAP enabled

How to add SWAP

First as you mentioned, we’ll add 3GB of SWAP

sudo fallocate -l 1G /swapfile

If faillocate is not installed, you’ll need to do

yum update
yum install faillocate

Now that you have the swapfile, you’ll need to set the right permissions. Please note only the root user should/will have access to the file

sudo chmod 600 /swapfile

Set up a Linux swap area.

sudo mkswap /swapfile

Enable the swap


sudo swapon /swapfile

To make the change permanent open the /etc/fstab file and append the following line:

/swapfile swap swap defaults 0 0

Verify the swap status.

sudo swapon --show

Now your server has SWAP.

As I saw, you want to have different priorities on different SWAP. The default SWAP priority is -1. If you have two swap partitions one with priority 10 and one with priority -1 the swap partion with priority 10 will be used first.

To change the priority of a swap partions, you can

swapon -p 10 /path/to/swap file/partion

That’s it, you now have everything configured as you wished

Kind regards, Kalin D.

KFSys
Site Moderator
Site Moderator badge
May 20, 2022

Hello all,

Apart from from CentOS, you can create SWAP on any distribution you want. Here is a link to a tutorial on how to add SWAP on an Ubuntu droplet:

https://www.digitalocean.com/community/tutorials/how-to-add-swap-space-on-ubuntu-20-04

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel