How to Enable Swap in Ubuntu

Swapping is the process of moving data from a system’s memory (RAM) to a preconfigured space on the hard drive, known as the swap space. Enabling swap, in many cases, is an easy and cheap way to squeeze a little more from a server. The most beneficial feature of swap is that it allows your system to use more memory resources than you have physical RAM.

A perfect example of how this is helpful comes from real life. The other day I decided to revisit the ownCloud project to see how it had matured since I last tried it. For my test platform I spun up a small DigtialOcean Droplet with 512mb of RAM. During my initial syncing of files MySQL kept exhausting all of the RAM, crashing, and bringing the whole ownCloud service to its knees. Even after tweaking MySQL the problem still persisted. Then it dawned on me that, like most virtual machines, DigitalOcean Droplets don’t have swap enabled by default. So, instead of adding more physical ram (ie: costing more money) I enabled swap, and the problem went away.

Things You Should Know

  • Like with most things, swap is not a magic cure or silver bullet. Accessing the disk is much slower than accessing RAM, so using swap comes with a performance trade-off.
  • Swap is allocated from your hard drive space. You should take into consideration how much free space you have, and realize that swap is going to take away some of that space.
  • How much swap space you should have is a matter of opinion and debate, but the general consensus is a 1:1 or 2:1 ratio of swap to RAM. For example, if you have 4GB of physical RAM then you’d want 4GB of swap for a 1:1 ratio, or 8GB for a 2:1 ratio. I’m pretty loose with this “rule”, and tend to base my decision on however much swap a particular job needs versus how much free HDD space the system has.
  • For this tutorial we’re going to add 4GB of swap, and mount it at /swapspace.

Step 1 – Preallocate the Swap Space

Step 2 – Setup the Swap Space

Step 3 – Enable Swap

Step 4 – Mount Swap at Boot

To make the swap permanent it needs to be mounted when the system starts up. Add the following entry to /etc/fstab:

Configure Swappiness

This step is optional, but you may find that it helps.

Swappiness is the setting that determines how often the systems swaps data from RAM to the swap space. Values represent percentages, and thus range from 0 to 100.

  • Values closer to 0 = System won’t swap unless absolutely necessary
  • Values closer to 100 = Favor putting more data into swap to keep RAM free

For servers and virtual machines a lower value (0-20) is usually better in most cases. For desktops a mid-range value (50-60) is probably more suitable. Since it’s “expensive” to interact with swap (when compared to RAM), crucial tasks or those most affected by latency might be better off with a lower swappiness value.

To determine the system’s current swappiness value run:

Set the swappiness to 10:

Make the changes permanent:

Leave a Reply

Your email address will not be published. Required fields are marked *

×Mike Everhart

Need Some More Help? Let's Talk!

I'd love to work with you! Fill out the form below to schedule a free consultation to discuss your needs and how I can help.

Need More Help?