System Administration Guide: Devices and File Systems

Chapter 20 Configuring Additional Swap Space (Tasks)

This chapter provides guidelines and step-by-step instructions for configuring additional swap space after the Oracle Solaris OS is installed.

This is a list of the step-by-step instructions in this chapter.

This is a list of the overview information in this chapter.

About Swap Space

You should understand the features of the SunOS swap mechanism to determine the following:

Swap Space and Virtual Memory

Solaris software uses some disk slices for temporary storage rather than for file systems. These slices are called swap slices. Swap slices are used as virtual memory storage areas when the system does not have enough physical memory to handle current processes.

The virtual memory system maps physical copies of files on disk to virtual addresses in memory. Physical memory pages that contain the data for these mappings can be backed by regular files in the file system, or by swap space. If the memory is backed by swap space it is referred to as anonymous memory because no identity is assigned to the disk space that is backing the memory.

The Oracle Solaris OS uses the concept of virtual swap space, a layer between anonymous memory pages and the physical storage (or disk-backed swap space) that actually back these pages. A system's virtual swap space is equal to the sum of all its physical (disk-backed) swap space plus a portion of the currently available physical memory.

Virtual swap space has these advantages:

Swap Space and the TMPFS File System

The TMPFS file system is activated automatically in the Solaris environment by an entry in the /etc/vfstab file. The TMPFS file system stores files and their associated information in memory (in the /tmp directory) rather than on disk, which speeds access to those files. This feature results in a major performance enhancement for applications such as compilers and DBMS products that use /tmp heavily.

The TMPFS file system allocates space in the /tmp directory from the system's swap resources. This feature means that as you use up space in the /tmp directory, you are also using up swap space. So, if your applications use the /tmp directory heavily and you do not monitor swap space usage, your system could run out of swap space.

Do use the following if you want to use TMPFS, but your swap resources are limited:

Swap Space as a Dump Device

A dump device is usually disk space that is reserved to store system crash dump information. By default, a system's dump device is configured to be a swap slice in a UFS root environment. If possible, you should configure an alternate disk partition as a dedicated dump device instead to provide increased reliability for crash dumps and faster reboot time after a system failure. You can configure a dedicated dump device by using the dumpadm command. For more information, see Chapter 17, Managing System Crash Information (Tasks), in System Administration Guide: Advanced Administration.

In a ZFS root environment, swap and dump are configured as separate ZFS volumes. The advantages to this model are as follows:

For more information about using ZFS swap and dump devices, see ZFS Support for Swap and Dump Devices in Oracle Solaris ZFS Administration Guide.

If you are using a volume manager to manage your disks in a UFS environment, such as Solaris Volume Manager, do not configure your dedicated dump device to be under its control. You can keep your swap areas under Solaris Volume Manager's control, which is a recommended practice. However, for accessibility and performance reasons, configure another disk as a dedicated dump device outside of Solaris Volume Manager's control.

Swap Space and Dynamic Reconfiguration

A good practice is to allocate enough swap space to support a failing CPU or system board during dynamic reconfiguration. Otherwise, a CPU or system board failure might result in your host or domain rebooting with less memory.

Without having this additional swap space available, one or more of your applications might fail to start due to insufficient memory. This problem would require manual intervention either to add additional swap space or to reconfigure the memory usage of these applications.

If you have allocated additional swap space to handle a potential loss of memory on reboot, all of your intensive applications might start as usual. This means the system will be available to the users, perhaps possibly slower due to some additional swapping.

For more information, see your hardware dynamic reconfiguration guide.

Configuring Swap Space in a SAN Environment

Review the following points to determine whether you might configure swap space on a network-connected disk, such as in a SAN environment:

How Do I Know If I Need More Swap Space?

Use the swap -l command to determine if your system needs more swap space.

For example, the following swap -l output shows that this system's swap space is almost entirely consumed or at 100% allocation.


% swap -l
swapfile             dev   swaplo blocks   free
/dev/dsk/c0t0d0s1   136,1      16 1638608    88

When a system's swap space is at 100% allocation, an application's memory pages become temporarily locked. Application errors might not occur, but system performance will likely suffer.

For information on adding more swap space to your system, see How to Create a Swap File and Make It Available in UFS Root Environment.

Swap-Related Error Messages

These messages indicate that an application was trying to get more anonymous memory. However, no swap space was left to back it.


application is out of memory
 
malloc error O
 
messages.1:Sep 21 20:52:11 mars genunix: [ID 470503 kern.warning] 
WARNING: Sorry, no swap space to grow stack for pid 100295 (myprog)

TMPFS-Related Error Messages

The following message is displayed if a page could not be allocated when a file was being written. This problem can occur when TMPFS tries to write more than it is allowed or if currently executed programs are using a lot of memory.


directory: File system full, swap space limit exceeded

The following message means that TMPFS ran out of physical memory while attempting to create a new file or directory:


directory: File system full, memory allocation failed

For information on recovering from the TMPFS-related error messages, see tmpfs(7FS).

How Swap Space Is Allocated

Initially, swap space is allocated as part of the Solaris installation process. If you use the installation program's automatic layout of disk slices and do not manually change the size of the swap slice, the Solaris installation program allocates a default swap area of 512 MB for a UFS root environment. In a ZFS root environment, the default swap size is generally in the 512 MB to 2 GB range.

For general guidelines on allocating swap space, see Planning for Swap Space.

You can allocate additional swap space to the system by creating a swap file in a UFS root environment. Swap files are not supported in a ZFS root environment. For information about creating a swap file, see Adding More Swap Space.

Swap Areas and the /etc/vfstab File

After the system is installed, swap slices and swap files are listed in the /etc/vfstab file. They are activated by the /sbin/swapadd script when the system is booted.

An entry for a swap device in the /etc/vfstab file contains the following:

The file system that contains a swap file must be mounted before the swap file is activated. So, in the /etc/vfstab file, ensure that the entry that mounts the file system comes before the entry that activates the swap file.

Planning for Swap Space

The most important factors in determining swap space size are the requirements of the system's software applications. For example, large applications such as computer-aided design simulators, database management products, transaction monitors, and geologic analysis systems can consume as much as 200–1000 MB of swap space.

Consult your application vendors for swap space requirements for their applications.

If you are unable to determine swap space requirements from your application vendors, use the following general guidelines based on your system type to allocate swap space.

System Type 

Swap Space Size 

Dedicated Dump Device Size 

System with about 4 GB of physical memory 

1 GB 

1 GB 

Mid-range server with about 8 GB of physical memory 

2 GB 

2 GB 

High-end server with about 16 to 128 GB of physical memory 

4 GB 

4 GB 

High-end server with more than 128 GB of physical memory 

1/4 of physical memory size 

1/4 of physical memory size 


Note –

Crash dump content is compressed so the dump device does not have to be the same size as physical memory. By default, the dump content value is set to kernel pages. However, if the dump content value is set to dump all memory pages, then consider increasing the dump size to half the size of physical memory or more.


Allocating Swap Space for UFS-Based Systems

In addition to preceding general guidelines, consider allocating swap space or disk space for a UFS-based system for the following:

Allocating Swap Space for ZFS-Based Systems

During an initial installation of a ZFS root file system or a Solaris Live Upgrade from a UFS file system to a ZFS root file system, a swap area is automatically created on a ZFS volume in the ZFS root pool, generally in the 512 MB to 2 GB range. In the case of a UFS to ZFS migration by using Solaris Live Upgrade, a swap volume is created based on the sizes of existing swap areas.

In a ZFS root pool, swap devices are not pre-allocated to fixed-size slices, so it is fairly easy to modify the swap size later.

After you assess the swap requirements of your applications, you can use the default swap size or adjust the swap volume size during an initial installation or after the installation, if necessary.

During an initial installation, the default dump volume size is calculated by the kernel based on dumpadm information and the size of physical memory. During a Live Upgrade migration from a UFS root file system to a ZFS root file system, the default dump volume size is set to half the size of physical memory, between 512 MB and 2 GB, in the ZFS BE.

In a ZFS environment, file systems consume space from the pool so the /var/crash directory consumes what it needs depending on how many crash dumps are saved.

Monitoring Swap Resources

The /usr/sbin/swap command is used to manage swap areas. Two options, -l and -s, display information about swap resources.

Use the swap -l command to identify a system's swap areas. Activated swap devices or files are listed under the swapfile column. For example:


# swap -l
swapfile             dev  swaplo blocks   free
/dev/dsk/c0t0d0s1   136,1      16 1638608 1600528

On a system with a ZFS root file system, the swap -l command identifies similar output except that it identifies the ZFS volume path name. For example:


# swap -l
swapfile                  dev  swaplo blocks   free
/dev/zvol/dsk/rpool/swap 256,1      16 1058800 1058800

Use the swap -s command to monitor swap resources.


# swap -s
total: 57416k bytes allocated + 10480k reserved = 67896k used, 
833128k available

The used value plus the available value equals the total swap space on the system, which includes a portion of physical memory and swap devices (or files).

You can use the amount of available and used swap space (in the swap -s output) as a way to monitor swap space usage over time. If a system's performance is good, use swap -s to determine how much swap space is available. When the performance of a system slows down, check the amount of available swap space to determine if it has decreased. Then you can identify what changes to the system might have caused swap space usage to increase.

When using this command, keep in mind that the amount of physical memory available for swap usage changes dynamically as the kernel and user processes lock down and release physical memory.


Note –

The swap -l command displays swap space in 512-byte blocks. The swap -s command displays swap space in 1024-byte blocks. If you add up the blocks from swap -l and convert them to KB, the result is less than used + available (in the swap -s output). The reason is that swap -l does not include physical memory in its calculation of swap space.


The output from the swap -s command is summarized in the following table.

Table 20–1 Output of the swap -s Command

Keyword 

Description 

bytes allocated

The total amount of swap space in 1024-byte blocks that is currently allocated as backing store (disk-backed swap space). 

reserved

The total amount of swap space in 1024-byte blocks that is not currently allocated, but claimed by memory for possible future use. 

used

The total amount of swap space in 1024-byte blocks that is either allocated or reserved. 

available

The total amount of swap space in 1024-byte blocks that is currently available for future reservation and allocation. 

Adding More Swap Space

As system configurations change and new software packages are installed, you might need to add more swap space. The easiest way to add more swap space is to use the mkfile and swap commands to designate a part of an existing UFS or NFS file system as a supplementary swap area. These commands, described in the following sections, enable you to add more swap space without repartitioning a disk.

Alternative ways to add more swap space are to repartition an existing disk or to add another disk. For information on how to repartition a disk, see Chapter 10, Managing Disks (Overview).

Creating a Swap File in a UFS Root Environment

You can create a swap file to be used in a UFS root file system. Swap files are currently not supported in a ZFS root environment. The following general steps are involved in creating a swap file:

mkfile Command

The mkfile command creates a file that is suitable for use as either an NFS-mounted swap area or a local swap area. The sticky bit is set, and the file is filled with zeros. You can specify the size of the swap file in bytes (the default) or in KB, blocks, or MB by using the k, b, or m suffixes, respectively.

The following table shows the mkfile command options.

Table 20–2 Options to the mkfile Command

Option 

Description 

-n

Creates an empty file. The size is noted. However, the disk blocks are not allocated until data is written to them. 

-v

Reports the names and sizes of created files. 


Note –

Use the -n option only when you create an NFS swap file.


ProcedureHow to Create a Swap File and Make It Available in UFS Root Environment

  1. Become superuser.

    You can create a swap file without root permissions. However, to avoid accidental overwriting, root should be the owner of the swap file.

  2. Create a directory for the swap file, if needed.

  3. Create the swap file.


    # mkfile nnn[k|b|m] filename
    

    The swap file of the size nnn (in KB, bytes, or MB) with the filename you specify is created.

  4. Activate the swap file.


    # /usr/sbin/swap -a /path/filename
    

    You must use the absolute path name to specify the swap file. The swap file is added and available until the file system is unmounted, the system is rebooted, or the swap file is removed. Keep in mind that you cannot unmount a file system while some process or program is swapping to the swap file.

  5. Add an entry for the swap file to the /etc/vfstab file that specifies the full path name of the file, and designates swap as the file system type.


    /path/filename   -      -       swap     -     no     -
  6. Verify that the swap file is added.


    $ /usr/sbin/swap -l
    

    Note –

    If a swap file does not get activated, make sure that the following service is running:


    # svcs nfs/client
    STATE          STIME    FMRI
    enabled        14:14:34 svc:/network/nfs/client:default 


Example 20–1 Creating a Swap File and Making It Available in a UFS Root Environment

The following examples shows how to create a 100-MB swap file called /files/swapfile.


# mkdir /files
# mkfile 100m /files/swapfile
# swap -a /files/swapfile
# vi /etc/vfstab
(An entry is added for the swap file):
/files/swapfile   -      -       swap     -     no     -
# swap -l
swapfile             dev  swaplo blocks   free
/dev/dsk/c0t0d0s1   136,1      16 1638608 1600528
/files/swapfile        -       16 204784  204784

Adding or Changing Swap Space in an Oracle Solaris ZFS Root Environment

The following section describes how to add or change swap space in a ZFS root environment. See the previous sections to determine if your system or applications need additional swap space.

ProcedureHow to Add Swap Space in an Oracle Solaris ZFS Root Environment

  1. Become superuser.

  2. Identify the current swap volume.

    A swap volume cannot be removed if it is in use. You can tell if the current swap volume is in use by comparing the blocks identified in the blocks column and blocks identified in the free column. If the blocks in the two columns are equal, the swap area is not busy. For example:


    # swap -l
    swapfile                 dev  swaplo   blocks   free
    /dev/zvol/dsk/rpool/swap 256,1      16 1058800 1058800
  3. Select one of the following to resize the swap volume.

    1. If the current swap area is not in use, you can resize the size of the current swap volume.

      For example:


      # zfs get volsize rpool/swap
      NAME        PROPERTY  VALUE    SOURCE
      rpool/swap  volsize   517M     -
      # zfs set volsize=2g rpool/swap
      # zfs get volsize rpool/swap
      NAME        PROPERTY  VALUE    SOURCE
      rpool/swap  volsize   2G       -
    2. If the current swap area is in use, you can add another swap volume.

      For example:


      # zfs create -V 2G rpool/swap2
      
    3. Activate the second swap volume.

      For example:


      # swap -a /dev/zvol/dsk/rpool/swap2
      # swap -l
      swapfile                  dev  swaplo   blocks   free
      /dev/zvol/dsk/rpool/swap  256,1      16 1058800 1058800
      /dev/zvol/dsk/rpool/swap2 256,3      16 4194288 4194288

Removing a Swap File From Use

If you have unneeded swap space, you can remove it.

ProcedureHow to Remove Unneeded Swap Space

  1. Become superuser.

  2. Remove the swap space.


    # /usr/sbin/swap -d /path/filename
    

    The swap file name is removed so that it is no longer available for swapping. The file itself is not deleted.

  3. Edit the /etc/vfstab file and delete the entry for the swap file.

  4. Recover the disk space so that you can use it for something else.


    # rm /path/filename
    

    If the swap space is a file, remove it. Or, if the swap space is on a separate slice and you are sure you will not need it again, make a new file system and mount the file system.

    For information on mounting a file system, see Chapter 18, Mounting and Unmounting File Systems (Tasks).

  5. Verify that the swap file is no longer available.


    # swap -l
    

Example 20–2 Removing Unneeded Swap Space

The following examples shows how to delete the /files/swapfile swap file.


# swap -d /files/swapfile
# (Remove the swap entry from the /etc/vfstab file)
# rm /files/swapfile
# swap -l
swapfile             dev  swaplo  blocks   free
/dev/dsk/c0t0d0s1   136,1      16 1638608 1600528