11.12. Adding Swap Space
Prev | Chapter 11. Configuration and Tuning | Next

## 11.12. Adding Swap Space

Sometimes a system requires more swap space. This section describes two methods to increase swap space: adding swap to an existing partition or new hard drive, and creating a swap file on an existing partition.

For information on how to encrypt swap space, which options exist, and why it should be done, refer to Section 17.13, “Encrypting Swap”.

### 11.12.1. Swap on a New Hard Drive or Existing Partition

Adding a new hard drive for swap gives better performance than using a partition on an existing drive. Setting up partitions and hard drives is explained in Section 17.2, “Adding Disks” while Section 2.6.1, “Designing the Partition Layout” discusses partition layouts and swap partition size considerations.

Use `swapon` to add a swap partition to the system. For example:

    # swapon /dev/ada1s1b

### Warning:

It is possible to use any partition not currently mounted, even if it already contains data. Using `swapon` on a partition that contains data will overwrite and destroy that data. Make sure that the partition to be added as swap is really the intended partition before running `swapon`.

To automatically add this swap partition on boot, add an entry to `/etc/fstab`:

    /dev/ada1s1b	none	swap	sw	0	0

See fstab(5) for an explanation of the entries in `/etc/fstab`. More information about `swapon` can be found in swapon(8).

### 11.12.2. Creating a Swap File

These examples create a 64M swap file called `/usr/swap0` instead of using a partition.

Using swap files requires that the module needed by md(4) has either been built into the kernel or has been loaded before swap is enabled. See Chapter 8, Configuring the FreeBSD Kernel for information about building a custom kernel.

Example 11.2. Creating a Swap File on FreeBSD 10.`X` and Later

1. Create the swap file:

       # dd if=/dev/zero of=/usr/swap0 bs=1m count=64

2. Set the proper permissions on the new file:

       # chmod 0600 /usr/swap0

3. Inform the system about the swap file by adding a line to `/etc/fstab`:

       md99	none	swap	sw,file=/usr/swap0,late	0	0

   The md(4) device `md99` is used, leaving lower device numbers available for interactive use.

4. Swap space will be added on system startup. To add swap space immediately, use swapon(8):

       # swapon -aL

Example 11.3. Creating a Swap File on FreeBSD 9.`X` and Earlier

1. Create the swap file, `/usr/swap0`:

       # dd if=/dev/zero of=/usr/swap0 bs=1m count=64

2. Set the proper permissions on `/usr/swap0`:

       # chmod 0600 /usr/swap0

3. Enable the swap file in `/etc/rc.conf`:

       swapfile="/usr/swap0"   # Set to name of swap file

4. Swap space will be added on system startup. To enable the swap file immediately, specify a free memory device. Refer to Section 17.9, “Memory Disks” for more information about memory devices.

       # mdconfig -a -t vnode -f /usr/swap0 -u 0 && swapon /dev/md0

Prev | Up | Next
11.11. Tuning Kernel Limits | Home | 11.13. Power and Resource Management

All FreeBSD documents are available for download at https://download.freebsd.org/ftp/doc/

Questions that are not answered by the documentation may be sent to <freebsd-questions@FreeBSD.org>.
Send questions about this document to <freebsd-doc@FreeBSD.org>.