# 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`.

{% tabs %}
{% tab title="Bash" %}

```bash
sudo fallocate -l 200G /swapfile.img
```

{% endtab %}
{% endtabs %}

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

{% tabs %}
{% tab title="Bash" %}

```bash
sudo chmod 0600 /swapfile.img
```

{% endtab %}
{% endtabs %}

Format the file as swap using `mkswap`.

{% tabs %}
{% tab title="Bash" %}

```bash
sudo mkswap /swapfile.img
```

{% endtab %}
{% endtabs %}

### 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.

{% tabs %}
{% tab title="Bash" %}

```bash
sudo mkswap /dev/vdb1
```

{% endtab %}
{% endtabs %}

### Enable Swap Memory

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

If using swapfile:

{% tabs %}
{% tab title="Bash" %}

```bash
sudo swapon /swapfile.img
```

{% endtab %}
{% endtabs %}

If using dedicated partition:

{% tabs %}
{% tab title="Bash" %}

```bash
sudo swapon /dev/vdb1
```

{% endtab %}
{% endtabs %}

Finally, verify that the swap partition is active.

{% tabs %}
{% tab title="Bash" %}

```bash
sudo swapon -s
```

{% endtab %}
{% endtabs %}

### [Make the Swap File Permanent](https://www.digitalocean.com/community/tutorials/how-to-add-swap-space-on-ubuntu-22-04#step-5-making-the-swap-file-permanent)

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.

{% tabs %}
{% tab title="Bash" %}

```bash
sudo cp /etc/fstab /etc/fstab.bak
```

{% endtab %}
{% endtabs %}

Now add the swap file to fstab.

{% tabs %}
{% tab title="Bash" %}

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

{% endtab %}
{% endtabs %}

## 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.

### Recommended Swappiness Value

For <code class="expression">space.vars.product\_name</code> 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`
