Configure Swap

This document describes the steps to configure swap memory and swapiness on a Ubuntu server.

Configure Swap Memory

We recommend configuring your swap memory size to be 1.5x of your host physical RAM for best performance.

There are two ways to configure swap memory for your host. You can either create a swap file in an existing partition, or allocate a dedicated disk partition for swap.

Configure Swap Memory using Swapfile

Create a new Swapfile with a specific size using fallocate.

sudo fallocate -l 200G /swapfile.img

Modify the Swapfile permissions to allow only the root user to read and write changes on the file.

sudo chmod 0600 /swapfile.img

Format the file as swap using mkswap.

sudo mkswap /swapfile.img

Configure Swap Memory using Disk Partition

Convert your designated block storage partition to swap. In the example below, /dev/vdb1 will be dedicated as your swap partition.

sudo mkswap /dev/vdb1

Enable Swap Memory

Once you have configured your swap memory, the next step is to enable swap using the swapon command.

If using swapfile:

sudo swapon /swapfile.img

If using dedicated partition:

sudo swapon /dev/vdb1

Finally, verify that the swap partition is active.

sudo swapon -s

If using swap file, you will need to add the swap file to your /etc/fstab file, so that the swap settings are retained across server reboot.

Backup your fstab file first.

sudo cp /etc/fstab /etc/fstab.bak

Now add the swap file to fstab.

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Configure Swappiness

What is Swappiness

Swappiness is a linux kernel parameter that determines the system's tendency to move data from physical RAM to the swap space on a disk. It's a value between 0 and 100.

Default swappiness value for Ubuntu is 60.

For Private Cloud Director hypervisors, we recommend setting the swappiness value to 10, to minimize swapping and favor keeping processes in RAM to avoid performance degradation.

A swappiness value of 10 means swapping is only used as a last resort when RAM is nearly exhausted, prioritizing the performance and responsiveness of VMs

Setting swappiness higher increases the likelihood of swap usage, which is not ideal for hypervisors due to the significant latency swap imposes on VM workloads.

Configure Swappiness

Use the following command to configure swappiness on your Ubuntu server.

Step 1 - Edit /etc/sysctl.conf

Step 2 - Add vm.swappiness = 10

Step 3 - Apply the changes by running sudo sysctl -p

Step 4 - Confirm that the changes are applied by running cat /proc/sys/vm/swappiness

Last updated

Was this helpful?