---
title: How to Add Swap Memory on FreeBSD 14.0
url: https://docs.vultr.com/how-to-add-swap-memory-on-freebsd-14-0
description: Learn how to add swap memory on FreeBSD 14.0 to improve system performance. Step-by-step guide for creating and configuring swap space on your FreeBSD system.
publish_date: 2024-09-11T12:11:00.854006Z
last_updated: 2024-10-21T18:45:41.199955Z
author: José Rafael Gutierrez
header_image: https://docs.vultr.com/public/doc-assets/1921/3c15ef91-61df-488b-ae3e-ed8a030a8635.png
---

> Index of every Vultr Docs page in markdown: https://docs.vultr.com/llms.txt

## Introduction

Swap space extends the capacity of your system's memory to improve performance when the RAM is full. During a memory shortage, your server stores inactive processes to the swap memory to run your system smoothly. Swapping processes prevents system shutdowns and crashes, especially when running multiple applications.

This article explains how to add a swap memory on FreeBSD 14.0.

## Prerequisites

Before you begin:

* Deploy a [FreeBSD 14.0](https://www.vultr.com/servers/freebsd) Cloud Compute instance on Vultr.
* Access the instance using [SSH](https://docs.vultr.com/how-to-use-ssh-with-vultr-servers) as a non-root user with sudo privileges.
* Attach a [Vultr Block Storage](https://www.vultr.com/products/block-storage/) volume to the FreeBSD 14.0 instance. You'll use this storage as a swap memory.

## Check Existing Swap Memory

Before adding a swap memory, verify if there is any existing swap memory on the system by running the following command.

```console
$ swapinfo
```

Output:

```
Device          1K-blocks     Used    Avail Capacity
```

The above command displays an empty swap information.

## Create Swap Memory

You can create swap memory using:

* A Swapfile
* A Vultr Block Storage volume

### Swapfile

1. Create a new swap file.

    ```console
    $ sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
    ```

    The above command creates a new Swapfile with 2 GB (`bs=1M count=2048`).

1. Create a new memory block device that points to the file.

    ```console
    $ sudo mdconfig -a -t vnode -f /swapfile -u 0
    ```

    Output:

    ```
    2048+0 records in
    2048+0 records out
    2147483648 bytes transferred in 0.884273 secs (2428530534 bytes/sec)
    ```

    The above command creates a memory block device associated with the `/swapfile` file and assigns it to the `/dev/md0` device.

1. Enable the memory block device as a swap.

    ```console
    $ sudo swapon /dev/md0
    ```

    The above command activates the Swapfile's block device as swap memory.

### Vultr Block Storage

Vultr Block Storage devices attached to your server use the `vtbd` naming scheme where the first block device is `vtbd1` and any additional devices use incremented values such as `vtbd2` for a second device. Follow the steps below to create swap memory using the Vultr Block Storage device attached to your server.

1. Initialize the block storage device attached to your server with a GPT partition table.

    ```console
    $ sudo gpart create -s GPT vtbd1
    ```

    Output:

    ```
    vtbd1 created
    ```

1. Add a new `swap0` partition to the device and use the partition as a swap space.

    ```console
    $ sudo gpart add -t freebsd-swap -l swap0 vtbd1
    ```

    Output:

    ```
    vtbd1p1 added
    ```

1. Format and enable the swap partition:

    ```console
    $ sudo swapon /dev/vtbd1p1
    ```

## Configure the File System Table (fstab) for Automatic Mounting

Automatic mounting enables the server to mount the swap memory at boot. Follow the steps below to enable automatic mounting of your swap memory created from a Swapfile or a Vultr Block Storage device.

### Enable Automatic Mounting for a Swapfile

Modify the `/etc/fstab` file to include your Swapfile's block device.

```console
$ echo "/dev/md0 none swap sw 0 0" | sudo tee -a /etc/fstab
```

Output:

```
/dev/md0 none swap sw 0 0
```

### Enable Automatic Mounting For Vultr Block Storage

Modify the `/etc/fstab` file to include your Vultr Block Storage device path.

```console
$ sudo echo "/dev/vtbd1p1 none swap sw 0 0" | tee -a /etc/fstab
```

Output:

```
/dev/vtbd1p1 none swap sw 0 0
```

## Test the Swap Memory

Follow the steps below to ensure the swap memory is actively swapping processes.

1. View the available swap space.

    ```console
    $ swapinfo
    ```

    Output:

    ```
    Device          1K-blocks     Used    Avail Capacity
    /dev/md0          2097152        0  2097152     0%
    ```

1. View the swap memory usage using the `top` utility.

    ```console
    $ top
    ```

    Output:

    ```
    last pid:  1362;  load averages:  0.12,  0.12,  0.10                    up 0+00:53:25  17:45:17
    24 processes:  1 running, 23 sleeping
    CPU:  0.0% user,  0.0% nice,  0.6% system,  0.0% interrupt, 99.4% idle
    Mem: 11M Active, 1868M Inact, 326M Wired, 206M Buf, 5708M Free
    Swap: 2048M Total, 2048M Free
    
      PID USERNAME    THR PRI NICE   SIZE    RES STATE    C   TIME    WCPU COMMAND
      ...
    ```

    Swap memory actively swaps processes and includes `2048` free memory based on the above `top` output.

## Remove Swap Memory

Follow the steps below when detaching swap memory from your server.

### Remove Swapfile

1. Disable the swap device.

    ```console
    $ sudo swapoff /dev/md0
    ```

1. Delete the memory block device.

    ```console
    $ sudo mdconfig -d -u 0
    ```

1. Delete the swap file.

    ```console
    $ sudo rm /swapfile
    ```

1. Remove the entry from `/etc/fstab` to disable automatic mounting.

    ```console
    $ sudo sed -i '' '/\/dev\/md0/d' /etc/fstab
    ```

### Remove Block Storage

1. Disable the swap partition.

    ```console
    $ sudo swapoff /dev/vtbd1p1
    ```

1. Remove the swap partition entry from `/etc/fstab`.

    ```console
    $ sudo sed -i '' '/\/dev\/vtbd1p1/d' /etc/fstab
    ```

## Conclusion

In this article, you have added a swap memory on a FreeBSD 14.0 to allow your system to swap processes. You've learned to use a Swapfile or a Vultr Block Storage volume to extend your system's memory. For more information, visit the [FreeBSD swap documentation](https://docs.freebsd.org/en/books/arch-handbook/vm/).
