Previous Page
Next Page

The Swap File System


Explain virtual memory concepts and, given a scenario, configure and manage swap space.

Physical memory is the random-access memory (RAM) installed in a computer. To view the amount of physical memory installed in your computer, type the following:

prtconf| grep "Memory size"

The system displays a message similar to the following:

Memory size: 384 Megabytes

Not all physical memory is available for Solaris processes. Some memory is reserved for kernel code and data structures. The remaining memory is referred to as available memory. Processes and applications on a system can use available memory.

Physical memory is supplemented by specially configured space on the physical disk that is known as swap space; together they are referred to as virtual memory. Swap space is configured either on a special disk partition known as a swap partition or on a swap file system (swapfs). In addition to swap partitions, special files called swap files can also be configured in existing Unix file systems (UFS) to provide additional swap space when needed.

Every process running on a Solaris system requires space in memory. Space is allocated to processes in units known as pages. Some of a process's pages are used to store the process executable, andand other pages are used to store the process's data.

Physical memory is a finite resource on any computer, and sometimes there are not enough pages in physical memory for all of a system's processes. When a physical memory shortfall is encountered, the virtual memory system begins moving data from physical memory out to the system's configured swap areas. When a process requests data that has been sent to a swap area, the virtual memory system brings that data back into physical memory. This process is known as paging.

The Solaris virtual memory system maps the files on disk to virtual addresses in memorythis is referred to as virtual swap space. As data in those files is needed, the virtual memory system maps the virtual addresses in memory to real physical addresses in memory. This mapping process greatly reduces the need for large amounts of physical swap space on systems with large amounts of available memory.

The virtual swap space provided by swapfs reduces the need for configuring large amounts of disk-based swap space on systems with large amounts of physical memory. This is because swapfs provides virtual swap space addresses rather than real physical swap space addresses in response to the requests to reserve swap space.

With the virtual swap space provided by swapfs, real disk-based swap space is required only with the onset of paging, because when paging occurs, processes are contending for memory. In this situation, swapfs must convert the virtual swap space addresses to physical swap space addresses in order for paging to actual disk-based swap space to occur.

Swap Space and TMPFS

The temporary file system (TMPFS) makes use of virtual memory for its storagethis can be either physical RAM or swap space; it is transparent to the user. /tmp is a good example of a TMPFS file system where temporary files and their associated information are stored in memory (in the /tmp directory) rather than on disk. This speeds up access to those files and results in a major performance enhancement for applications such as compilers and database management system (DBMS) products that use /tmp heavily.

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

Sizing Swap Space

The amount of swap space required on a system is based on the following criteria:

  • Application programs need a minimum amount of swap space to operate properly. This information is usually contained in the documentation that comes with the application. You should follow the manufacturer's recommendation for swap space requirements.

  • You need to determine whether large applications (such as compilers) will be using the /tmp directory. Then you need to allocate additional swap space to be used by TMPFS.

  • To prevent any possible panic dumps resulting from fatal system failures, there must be sufficient swap space to hold the necessary kernel memory pages in RAM at the time of a failure. Kernel memory accounts for around 20% of total memory, so if you have 1GB of physical memory, you will need about 256MB of disk-based space for a worst-case crash dump.


Movement of Swap Starting with the release of Solaris 9, the installation program allocates swap at the first available cylinder on the disk (this is normally cylinder 0). This practice allows the root file system the maximum space on the disk and allows for expansion of the file system during an upgrade.

The amount of disk-based swap space on a system must be large enough to be able to accommodate a kernel memory dump, plus the requirements of any concurrently running processes, including third-party applications and compilers. Many other factors also contribute to the amount of swap space you need to configure, such as the number of concurrent users and the naming service, Network Information System Plus (NIS+). It is quite rare nowadays to need more swap space than RAM, which used to be a recommendation with older versions of SunOS; in fact the opposite is often trueyou now often need less swap space than physical RAM.

If you are prepared to keep track of your swap space and administer it regularly, you can run with much less swap space than in older versions of SunOS. (How to monitor swap space and how to add additional space to a running system are discussed in the next few sections.)


Reducing Swap Space Problems If the amount of swap space is equal to the amount of physical RAM, you should generally experience no swap space problems, although the type of application being used on the system will be a major factor.

Monitoring Swap Resources

If you run into a swap shortfall due to heavy demand on memory, you get error messages on your system's console. The error might look something like this:

<application> is out of memory
 malloc error O
 messages.1:SJul 18 15:12:47 ultra genunix: [ID 470503 kern.warning]
WARNING: Sorry, no swap space to grow stack for pid 100295 (myprog)

This error means that an application is trying to get more memory and there is no swap space available to back it.

You could fill up a TMPFS due to the lack of available swap and get the following error message:

<directory>: File system full, swap space limit exceeded

or this one:

<directory>: File system full, memory allocation failed

This type of message is displayed if a page cannot be allocated when a file is being written. This can occur, for example, when TMPFS tries to write more than it is allowed or when TMPFS runs out of physical memory while attempting to create a new file or directory.

You need to regularly monitor your swap space. This helps you determine whether you are running on the edge and need to increase the resource or maybe you have too much swap space allocated and are wasting disk space. Most commercial performance monitoring tools keep track of swap space or can be configured to generate warnings when it gets low. Besides these commercial tools, you can use the helpful tools that Solaris provides (see Table 9.1). System performance monitoring is not covered on the administrator certification exams, so this chapter describes only the /usr/sbin/swap command.

Table 9.1. Swap Monitoring Tools




The /usr/sbin/swap utility provides a method for adding, deleting, and monitoring the system swap areas used by the memory manager.


You can use the -al options with the /usr/bin/ps command to report the total size of a process that is currently in virtual memory. The value includes all mapped files and devices, and it is reported in pages. These device mappings do not use swap space.


You can use this Berkley version of the ps command with the -alx options to report the total size of a process that is currently in virtual memory. The value includes all mapped files and devices, and it is reported in kilobytes rather than pages.


This tool reports virtual memory statistics.


This is a system activity reporter.

You can use two options with the /usr/sbin/swap command to monitor swap space. You can use the -l option to list swap space and to determine the location of a system's swap areas:

swap -l

The system displays details of the system's physical swap space. This system has a 512MB swap slice allocated, as shown below:

swapfile            dev  swaplo blocks   free
/dev/dsk/c0t0d0s1  136,9     16 1049312  1049312

This output is described in Table 9.2.

Table 9.2. Output from the swap -l Command[*]




The pathname for the swap area (for example, /dev/dsk/c0t0d0s1).


The major/minor device number for a block special device; this value is zeros otherwise.


The swaplo value for the area, in 512-byte blocks. swaplo is a kernel parameter that you can modify, and it represents the offset, in 512-byte blocks, where usable swap space begins.


The swaplen value for the area, in 512-byte blocks. swaplen is a kernel parameter that you can modify, and it defines the size of the swap area, in 512-byte blocks.


The number of 512-byte blocks in this area that are not currently allocated.

[*] This table does not include swap space in the form of physical memory because that space is not associated with a particular swap area.

You use the -s option to list a summary of the system's virtual swap space:

swap -s

The system displays the following information, which shows the details of the system's physical swap space and includes physical memory too. This system has 384MB of physical memory and a 512MB swap slice:

total: 67648k bytes allocated + 19032k reserved = 86680k used, 724512k \

This output is described in Table 9.3

Table 9.3. Output from the swap -s Command



bytes allocated

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


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


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


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

You can use the amounts 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, you can use swap -s to see how much swap space is available. When the performance of a system slows down, you can 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 the swap command that the amount of physical memory available for swap usage changes dynamically as the kernel and user processes reserve and release physical memory.


Swap Space Calculations The swap -l command displays swap space in 512-byte blocks, and the swap -s command displays swap space in 1,024-byte blocks. If you add up the blocks from swap -l and convert them to kilobytes, you'll see that it is less than the swap space used plus available (as shown in the swap -s output) because swap -l does not include physical memory in its calculation of swap space.

Setting Up Swap Space

Swap space is initially configured during software installation through the installation program. 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 slice of 512MB.


Crash Dumps As described later in this chapter, a crash dump is a disk copy of the kernel memory of the computer at the time of a fatal system error. When a fatal operating system error occurs, a message describing the error is printed to the console. The operating system then generates a crash dump by writing the contents of kernel memory to a predetermined dump device, which is typically a local disk partition. You can then analyze this crash dump to determine the cause of the system error. By default, the dump device is configured to be an appropriate swap partition. Therefore, it's necessary to make sure that your swap area is at least as large as about 25% of your physical RAM; otherwise, the system may not have enough room to store the crash dump. Crash dumps and core files are discussed later in this chapter, in the sections "Core File Configuration" and "Crash Dump Configuration."

The software installation program adds entries for swap slices and files in the /etc/vfstab file. These swap areas are activated each time the system is booted by /sbin/swapadd.

As system configurations change, more users are added, and new software packages are installed, you might need to add more swap space. There are two methods for adding more swap to a system:

Creating a secondary swap partition requires additional, unused disk space. You use the format command as described in Chapter 1, "Managing File Systems," to create a new partition and file system on a disk. After you create the swap partition, you make an entry in the /etc/vfstab file so that the swap space is activated at bootup. The process is described in Step by Step 9.1.

Step By Step 9.1: Creating a Secondary Swap File

Add an additional 512MB of swap space to your system. You don't have any more room on the disk for more swap space, but the /data directory (currently mounted on slice 4 of disk c0t1d0) is 512MB in size. Move all the data in /data to another server to free up the partition so that you can use it as a swap partition. You can use any one of the methods described in Chapter 7, "Performing System Backups and Restorations," to do this.

After freeing up the /data directory and unmounting /dev/dsk/c0t1d0s4, use the format utility to set the tag name to swap and the permission flag to wu (writable and unmountable), as follows:

partition> 4
Part     Tag      Flag  Cylinders    Size      Block
4     unassigned  wm    3400 - 4480  512.37MB (1041/0/0)   1049328
Enter partition id tag[unassigned]: swap
Enter partition permission flags[wm]: wu
Enter new starting cyl[3400]: <return>
Enter partition size[1049328b, 1041c, 1040e, 512.37mb, 0.50gb]:

The bold text here identifies the keystrokes entered by the user during execution of the command.

Label the disk, as follows:

Partition> la
Ready to label disk? Y

Make an entry to the /etc/vfstab file, where the fields are as follows:

Device to mount: <name of swap block device or swap file>

Device to fsck: --

Mount point: --

FS-type: swap

fsck pass: --

Mount at boot: no

Mount options: --

Here's an example of an entry for the swap partition just added:

/dev/dsk/c0t1d0s4     -       -       swap - no      -

Run the swapadd script to add the swap to your system:


Verify that the swap has been added:

swap -l

The system responds with this:

swapfile             dev  swaplo blocks   free
/dev/dsk/c0t0d0s1   136,9      16 1049312 1049312
/dev/dsk/c0t1d0s4   136,3      16 1052624 1052624

/dev/dsk/c0t1d0s4 has been added to the list of available swap areas.

Exam Alert

/etc/vfstab syntax You should be familiar with the entry for swap files in /etc/vfstab. The syntax can be tricky, especially because of the hyphens.

The following are additional notes that explain how to add swap partitions:

  • On systems running the 32-bit version of Solaris, swap areas must not exceed 2GB. If you wanted to add a 9GB disk to a swap area, you should slice it up into 2GB chunks. Then, you need to put a separate entry in /etc/vfstab for each slice. On systems running the 64-bit version of Solaris 10, you can use a block device larger than 2GB.

  • You get a large performance benefit from having swap partitions spread across separate disks. Swap space is allocated in a round-robin fashion from swap partition to swap partition, and it is not possible to prioritize usage of the various swap areas. Swap space is allocated 1MB at a time from each swap partition in turn, unless one is full.

  • It is not worth making a striped metadevice to swap on; that would just add overhead and slow down paging.

The easiest way to add more swap space is to use the mkfile and swap commands to designate a part of an existing UFS as a supplementary swap area. You can do this as a temporary or semi-temporary solution for a swap shortage. Although you can do this for longer durations as well, it has a few disadvantages:

  • A swap file is considered a file within a file system; therefore, when you back up a file system, a rather large swap file (empty file) is also backed up if you don't specifically exclude it.

  • Because a swap file is simply a file in some file system, you are not able to unmount that file system while the swap file is in use.

  • This method of creating a swap file has a negative effect on system performance because the swap file is slower than a dedicated swap slice.

Step by Step 9.2 explains how to add more swap space without repartitioning a disk.

Step By Step 9.2: Adding Swap Space Without Repartitioning a Disk

As root, use the df -h command to locate a file system that has enough room to support a swap file that's the size that you want to add:

# df -h
Filesystem             size   used  avail capacity  Mounted on
/dev/dsk/c0t0d0s0      4.9G   3.7G   1.2G    77%    /
/devices                 0K     0K     0K     0%    /devices
ctfs                     0K     0K     0K     0%    /system/contract
proc                     0K     0K     0K     0%    /proc
mnttab                   0K     0K     0K     0%    /etc/mnttab
swap                   1.2G   1.0M   1.2G     1%    /etc/svc/volatile
objfs                    0K     0K     0K     0%    /system/object
fd                       0K     0K     0K     0%    /dev/fd
/dev/dsk/c0t0d0s7      4.0G   1.5G   2.4G    40%    /var
swap                   1.2G   304K   1.2G     1%    /tmp
swap                   1.2G    48K   1.2G     1%    /var/run
/dev/dsk/c0t1d0s0      3.9G   1.7G   2.2G    44%    /data1
/dev/dsk/c0t1d0s7      5.2G   7.1M   5.1G     1%    /data2


Swap Permissions 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 prevent someone from accidentally overwriting it.

Use the mkfile command to add a 512MB swap file named swapfile in the /data2 partition:

mkfile 512m /data2/swapfile

Use the ls -l /data2 command to verify that the file has been created:

ls -l /data2/swapfile
-rw------T  1 root  root 536870912 Aug 19 23:31 /data2/swapfile

The system shows the file named swapfile along with the file size. Notice that the sticky bit (which is described in Chapter 4, "User and Security Administration") has automatically been set.

Activate the swap area by using the swap command:

/usr/sbin/swap -a /data2/swapfile

You must use the absolute pathname 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 the swap file is still being used or a process is swapping to the swap file.

Verify that the new swap area was added:

swap -l

The system should respond with a message such as the following that shows the swap file:

swapfile             dev  swaplo blocks   free
/dev/dsk/c0t0d0s1   136,9     16 1049312 1049312
/data2/swapfile        -       16 1048560 1048560

If this will be a permanent swap area, add to the /etc/vfstab file an entry for the swap file that specifies the full pathname of the swap file and designate swap as the file system type:

/data2/swapfile - - swap - no -

There is some disagreement as to which type of swap area provides the best performance: a swap partition or a swap file. There are factors in favor of both scenarios; however, these are two of the best reasons in favor of swap partitions:

  • A partition provides contiguous space and can be positioned between the specific cylinders that will provide the best performance.

  • A swap file has to work through the file system when updates are made, whereas a swap partition has data written to it at a lower level, bypassing the interaction with the file system; this makes a swap partition slightly faster than a swap file.

Sun's official statement, and the general consensus in the user community, is that there will be a performance impact if you go the swap file route rather than the partition route. Sun recommends that you use swap files only as a temporary solution, until you can add a swap partition.


Swap Files on NFS In an emergency, when no other local space is available, it's possible to add a swap file to a networked file system by using NFS; this is described later in this chapter. Using NFS to access swap space on another host is not recommended, however, because it puts an increased load on your network and makes performance unacceptable. If you do need to use NFS for additional swap files, try using the -n option when you run mkfile, as this will only allocate disk blocks as they are written.

Swap files can be deleted as well as added. For example, you might determine that you have allocated too much swap space and that you need that disk space for other uses. Alternatively the additional swap space might have been temporarily added to accommodate a one-off large job. The steps involved in removing a swap file are outlined in Step by Step 9.3.

Step By Step 9.3: Removing a Swap File

As root, use the swap -d command to remove the swap area:

swap -d /dev/dsk/c0t0d0s4   for a swap partition or,
swap -d /data2/swapfile            for a swap file.

Issue the swap -l command to ensure that the swap area is gone:

swap -l
swapfile             dev  swaplo blocks   free
/dev/dsk/c0t0d0s1   136,9      16 1049312 1049312

The swap file filename is removed from the list, so you know it is no longer available for swapping. The file itself is not deleted.

In the /etc/vfstab file, delete the entry for the swap file.

Remove the swap file to recover the disk space:

rm /data2/swapfile

If the swap area was in a partition, you can now allocate this disk space as you would a normal file system.

Previous Page
Next Page