Adding SWAP – also known as Virtual Memory

For computers, the internal memory is the primary location to store and process data. For smaller virtual servers this random access memory (RAM) is usually 512 MB or 1 GB.

Sometimes it might be necessary to free some of the internal memory. This is done by moving data form the internal memory to a predefined location on the hard disk.

This preconfigured part of the disk is what we call the swap area, also dubbed as virtual memory.

Swapping is the entire process of moving data from internal memory to the swap space and vice versa.

Servers tend to get slower when the entire memory is used, so a swap area affect the performance of your VPS positively.

This post will show you how can add swap to your server.

We achieve this in four steps:

  1. verify existence
  2. create swap
  3. make permanent
  4. a little tweaking

The last step is an optional.

This will be a rather technical post. The good news is, that you do not need to memorize it. Just get back here the next time you need to add swap space to a fresh installed virtual server.

Verify Existence

Prior to creating a swap area, we need to verify that there is not any swap space yet.

Some hosts automatically create a swap with every new VPS instance, others do not.

A simple way to verify the swap, is to check the available memory with the free command.

Enter at the prompt:

By default, the free command reports in bytes. Although understandable, we humans prefer lesser digits.

By adding the -h parameter, we ask for a more human readable form.

In case of a VPS with 512 MB memory, the system returns something like:

The last line indicates that there is no swap yet – there is a total of 0 bytes.

Create SWAP

Let us create a virtual memory of 1 GB with the file allocation command:

This command allocates a file of 1 GB, named swapfile and located in the root (/).

Now let us verify the existence of the file swapfile:

What we see is:

Now we have to remove the read permissions by the group and others (the second and third ‘r’):

Let us check again:

And we see:

This tells us that only the file owner – in this case the root – has read and write permissions. That is how we like it.

Enable SWAP

Okay, so now we have a file named swapfile, but we still have to tell the system to use that file as swap. Let us do just that:

This translates as make swap from the swapfile. The system replies with:

Turn the swap on:

The swapon and swapoff commands allow us to enable and disable the swap area.

We want the server to inform us about the status of the swap:

By adding the -s parameter, we ask for a summary of the swap space.

Let us check the memory again with the free command:

This time, the server returns:

Yep, now have a 1 GB swap space.

Make Permanent

To ensure us that the system recreates the swap space after a reboot, we have to add it to the fstab file, the file system table.

Let us open the fstab file with the Nano editor:

The Nano editor opens with the current fstab file:

We are going to add one line below the identification of the hard disk (the UUID):

Type or paste the line above. You put it below the line with the disk’s UUID.

In case you want to paste the line:

  1. copy it
  2. put the cursor on a new line beneath the UUID
  3. paste with Shft+Ctrl+V

The two last lines of the fstab should look something like this:

The ID number will probably be different from the one above. That is okay.

Save the file with Ctrl+O.

Exit with Ctrl+X.

The swap is now permanent and will be activated when the system reboots.

Tweaking the SWAP

Our swap is ready, but we like to tweak it a bit. This is an optional step, and you can execute it later if you want.

We want to improve swappiness and caching.

The swappiness defines how much we like the system to swap data.

A value of 0 indicates; do not swap, unless absolutely necessary.

A value of 100 assigns a high priority to swapping – swap as much as possible.

This works comparable regarding the caching of meta data.

Without further explanations, let us open the systemctl – the configuration file of the services manager.

Enter:

This file is relatively long, but starts as following:

Go with the cursor to the end of the file, and add these lines – replace jdoe by your own name:

Save the file with Ctrl+O.

Exit with Ctrl+X.

Done.

The line starting with the # is a comment. It is optional, but will tell you or your colleagues that it was you who added the following two lines.

We have changed the sysctl configuration file, but we need either to reboot the system or reload the sysctl.conf.

Reboots should be reduced to the absolute minimum, so we rather reload the file:

Now let us check whether the desired values have been applied correctly:

The reply confirms:

And when you enter:

The system returns:

This confirms that the tweaking was executed successfully.

In case you are wondering, the swappiness and vfs_cache_pressure files are located in a subdirectory of the proc folder.

This folder holds files with information about the processes on the server.

I warned you, this was going to be a rather technical post. The good news is; you will not be needing this info until the next time you create a swap area. Probably the next time you create a VPS.

More details