12.12. Adding Swap Space

Sometimes a system requires more swap space. There are three ways to increase swap space: add a new hard drive, enable swap over NFS, or create 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 18.14, “Encrypting Swap”.

12.12.1. Swap on a New or Existing Hard Drive

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

Use swapon(8) 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(8) 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(8).

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.

12.12.2. Swapping over NFS

Swapping over NFS is only recommended when there is no local hard disk to swap to. NFS swapping will be limited by the available network bandwidth and puts an additional burden on nfsd(8).

12.12.3. Swapfiles

To create a swap file, specify its size. The following example creates a 64MB file named /usr/swap0.

Example 12.2. Creating a Swapfile on FreeBSD
  1. The GENERIC kernel already includes the memory disk driver (md(4)) required for this operation. When building a custom kernel, make sure to include the following line in the custom configuration file:

    device   md

    For information on building a custom kernel, refer to Chapter 9, Configuring the FreeBSD Kernel.

  2. First, create the swapfile /usr/swap0:

    # dd if=/dev/zero of=/usr/swap0 bs=1024k count=64
  3. Then, set proper permissions on /usr/swap0:

    # chmod 0600 /usr/swap0
  4. Enable the swap file in /etc/rc.conf:

    swapfile="/usr/swap0"   # Set to name of swapfile if aux swapfile desired.
  5. Reboot the machine or, to enable the swap file immediately, type:

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

All FreeBSD documents are available for download at http://ftp.FreeBSD.org/pub/FreeBSD/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>.