Adding swap space on the fly
Posted by Mike C. on 03 January 2010 09:54 PM
All Unix systems support virtual memory, sometimes referred to simply as "VM". With VM in service, active applications and processes on your system can reside in physical memory or space on your hard disks that serves as an extension to physical memory. The amount of swap space (the portion of virtual memory that resides on disk) that you need on any particular server depends both on how much physical memory the system has and on how you use it. A server endowed with a copious amount of physical memory may rarely make use of any significant amount of swap. A server with only one or two gigabytes of memory may rely on swap to perform adequately.|
The primary advantage of swap is that it allows physical memory to be used more efficiently. Running processes and the data they use can be offloaded from physical memory and stored in swap until it is needed again. As a result, more applications that could effectively share physical memory can be handled as if run simultaneously. The larger addressable memory space can streamline development while providing good performance.
In general, the rule of thumb is to configure enough swap space to allow your system to perform well, while not so much that you short yourself on available disk space. You might try to allocate as much swap as you have physical memory or you might aim at a figure that brings your virtual memory to something between 4 and 8 GB on a development system or busy server.
An inadequate amount of memory can result in an inability to run as many applications or processes as you want to support. You might see errors such as "WARNING /tmp: File system full, swap space limit exceeded" or applications refusing to load. You might even see segmentation faults or core dumps.
Fortunately, adding memory on the fly is generally easy. On Solaris systems, you can add an unused partition to your swap space just by issuing the "swap -a partition" command (e.g., swap -a /dev/dsk/c1t0d0s4). Use the "swap -l" command to get a listing of your swap space.
If you don't have an unused partition, you can create a file using the mkfile command and swap on it. Here are some sample mkfile commands:
# mkfile 1024m /var/swapfile
# mkfile 2g /spare/bigswap
To add the new swap space to active swap, use "swap -a":
# swap -a /var/swapfile
# swap -a /spare/bigswap
Your new swap space should be available immediately.
To make the swap space addition permanent, you have to add it to /etc/vfstab.
/dev/dsk/c1t0d0s4 - - swap - no -
In spite of the "no" listed in the "mount at boot" column, swap space set up this way will be set up for use when the system reboots.
If you later feel that you've allocated too much disk space for your swapfile, you can remove it with a "swap -d" command and allocate a smaller amount.
Linux systems use a very similar set of commands for creating swap partitions. You can use the "mkswap" command as shown here and the "swapon" command to put the swap space into use:
# mkswap /dev/hda2
Setting up swapspace version 1, size = 2056314 kB
# swapon /dev/hda2
For swapfiles on Linux, a mkfile command (like that on Solaris) is generally available and "swap -a" or "swapon" can be used to make the swap space available for use.