System Administration Guide, Volume 1

Chapter 38 Configuring Additional Swap Space (Tasks)

This is a list of the overview conceptual information and step-by-step instructions in this chapter.

About Swap Space

It is important for administrators to understand the features of the SunOS swap mechanism in determining:

Swap Space and Virtual Memory

The 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 which 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 there is no identity assigned to the disk space backing the memory.

The Solaris environment 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 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 means that as you use up space in /tmp, you are also using up swap space. So if your applications use /tmp heavily and you do not monitor swap space usage, your system could run out of swap space.

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

How Do I Know If I Need More Swap Space?

This section lists several possible error messages displayed when you run out of swap space.

Swap-Related Error Messages

These messages indicate that an application was trying to get more anonymous memory and there was no swap space 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

directory: File system full, swap space limit exceeded

This message is displayed if a page could not be allocated when writing a file. This 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, memory allocation failed

This message means TMPFS ran out of physical memory while attempting to create a new file or directory.

See TMPFS(7FS) for information on recovering from the TMPFS-related error messages.

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 default swap slices as shown in the table below.

Table 38-1 Default Swap Space Allocations

If Your System Has n Mbytes of Physical Memory ...

Then the Default Swap Space Allocated Is ... 


32 Mbytes 


64 Mbytes 


128 Mbytes 

greater than 512 

256 Mbytes 

Additional swap space can also be added to the system by creating a swap file. See "Adding More Swap Space" for information about creating a swap file.

The /etc/vfstab File

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

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

Because the file system containing a swap file must be mounted before the swap file is activated, make sure that the entry that mounts the file system comes before the entry that activates the swap file in the /etc/vfstab 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 Mbytes of swap space.

Consult your application vendor for swap space requirements for any application whose data files typically exceed 10-20 Mbytes in size.

If you are unable to determine swap space requirements from the application vendor, use the following guidelines to allocate swap space:

Monitoring Swap Resources

The /usr/sbin/swap command is used to manage swap areas. Two options, -l and -s, are used to 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.

# swap -l
swapfile           dev  swaplo blocks   free
/dev/dsk/c0t2d0s1 32,17      8 205624 192704

Use the swap -s command to monitor swap resources.

# swap -s
total: 10492k bytes allocated + 7840k reserved = 18332k used, 21568k available

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

You can use the amount of swap space available and used (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 see how much swap space is available. When the performance of a system slows down, check the amount of swap space available to see if it has decreased. Then you can identify what changes to the system might have caused swap space usage to increase.

Keep in mind when using this command 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 and the swap -s command displays swap space in 1024-byte blocks. If you add up the blocks from swap -l and convert them to Kbytes, it will be less than used + available (in the swap -s output) because swap -l does not include physical memory in its calculation of swap space.

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

Table 38-2 Output of the swap -s Command



bytes allocated

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


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


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


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 below, 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 add another disk. See Chapter 28, Disk Management (Overview) for information on how to repartition a disk.

Creating a Swap File

The following general steps are involved in creating a swap file:

The mkfile Command

The mkfile command creates a file that is suitable for use either as an NFS-mounted or 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 kilobytes, blocks, or megabytes using the k, b, or m suffixes, respectively.

The table below shows the options to the mkfile command.

Table 38-3 Options to the mkfile Command




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


Verbose. Reports the names and sizes of created files. 

Caution - Caution -

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

How to Create a Swap File and Make It Available

  1. Become superuser.

    You can create a swap file without root permissions, but it is a good idea for root to be the owner of the swap file to avoid accidental overwriting.

  2. Create the swap file.

    # mkfile nnn[k|b|m] filename

    The swap file of the size nnn (in Kbytes, bytes, or Mbytes) and name you specify is created.

  3. 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 can't unmount a file system while some process or program is swapping to the swap file.

  4. 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, like this:

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

    $ /usr/sbin/swap -l

Example--Creating a Swap File and Making It Available

The following examples shows how to create a 24 Mbyte swap file called /files/swapfiles.

# mkdir /files
# mkfile 24m /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/c0t2d0s1   32,17      8 205624 192704
/files/swapfile       -        8  40952  40952

Removing a Swap File From Use

If the user no longer needs the extra swap space, you can remove it.

How to Remove Extra Swap Space

  1. Become superuser.

  2. Use the swap -d command to remove swap space.

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

    The swap file name is removed from the list 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 swap-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.

    See Chapter 36, Mounting and Unmounting File Systems (Tasks) for information on mounting a file system.

Example--Removing Extra Swap Space

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

# swap -d /files/swapfile
# (Remove the deleted swap entry from the /etc/vfstab file)
# rm /files/swapfile
# swap -l
swapfile             dev  swaplo  blocks   free
/dev/dsk/c0t2d0s1   32,17      8  205624  192720