The swap command is used to add virtual RAM to a system. Virtual RAM is typically used to provide memory for process execution when physical memory has been exhausted. Disk blocks are used to simulate physical memory locations using an interface that is invisible to the user. Thus, users never need to be concerned about the type of RAM that their process is addressing. While virtual memory allows a system’s effective capacity to be increased to many times its physical capacity, it is much slower than physical RAM. When a system experiences peak demands for memory, causing virtual memory to be used, the CPU must work harder to support virtual memory operations. Coupled with the relatively slow speed of disk writing, this has a significant impact on performance. When virtual memory is being utilized, and many new memory access calls are made along with normal file reading and writing, so-called "disk thrashing" can occur because the number of disk operations requested far exceeds the capacity of the disk to read and write.
If disk thrashing is a common occurrence, extra physical RAM should be installed into the system, and/or the file system may need to be tuned with tunefs.
Virtual memory should generally be added to the system at twice the physical RAM installed. Thus, for a 256MB system, 512MB of virtual memory should be initialized. In order to add virtual memory, an empty file of the required size should be created with the mkfile command. For example, to create two swap files with 4097072KB each, the following commands would be used:
# mkfile 4097072k /u1/swap # mkfile 4097072k /u2/swap
Next, the swap command must be used to add the file into the pool of available disk space. For example, if two swap files are created on different file systems for redundancy (such as /u1/swap and /u2/swap), they can be added to the swap space pool by using the following commands:
# swap -a /u1/swap # swap -a /u2/swap
To verify that the swap has been correctly added to the pool, the following command can be used:
# swap -l swapfile dev swaplo blocks free /dev/dsk/c0t0d0s1 118,17 16 8194144 6240336 /dev/dsk/c3t4d0s1 118,1 16 8194144 6236384
In this example, you can see that the partitions c0t0d0s1 and c3t4d0s1 have 8194144 blocks each allocated for swap, and have 6240336 and 6236384 free blocks, respectively.
A summary of the swap space can also be printed by using the swap –s command:
# /usr/sbin/swap -s total: 2360832k bytes allocated + 130312k reserved = 2491144k used, 7238792k available
In this example, you can see that 2360832K has been allocated, while 130312k has been reserved.
If you have a dedicated slice set aside for swap, the block device name can simply be passed on the command line:
# swap -a /dev/dsk/c1t1d2s1
To ensure that this partition is added as swap during boot, the following entry can be entered into the /etc/vfstab file:
#device device mount FS fsck mount mount #to mount to fsck point type pass atboot ops /dev/dsk/c1t1d2s1 swap no -
To remove a file (or device) from the swap pool, the -d option needs to be passed on the command line. Thus, to remove /u1/swap and /dev/dsk/c1t1d2s1 from the swap pool, the following commands would be used:
# swap -d /u1/swap # swap -d /dev/dsk/c1t1d2s1
The file /u1/swap can now be safely deleted, and the slice /dev/dsk/c1t1d2s1 can be safely used for other purposes, as long as the /etc/vfstab entries have been deleted.
An issue that commonly arises when swap partitions are enabled on production systems is whether or not swap space should be created on a mirrored partition (that is, RAID level 1). Mirroring ensures that when data is written to a partition on one disk it is also copied in full to a sister partition on another drive. This ensures that if data on the first drive is destroyed, it can be recovered automatically from the mirrored volume.
Creating swap files on mirrored partitions ensures that virtual memory cannot be corrupted by a disk failure. Thus, if a disk containing virtual memory for a production system is corrupt while executing a critical application, such as a database server, then the correct data will automatically be read from the mirrored volume if corruption is detected. However, since RAID mirroring requires that all data written to the source volume also be written immediately afterwards to the mirrored volume, this can significantly slow down effective write speeds for the entire system—because data must be written twice.