Creating dynamic swap space

Posted by Steve on Thu 27 Sep 2007 at 06:32

When a GNU/Linux machine runs out of physical memory it will start to use any configured swap-space. This is usually a sign of trouble as swap files and partitions are significantly slower to access than physical memory, however having some swap is generally better than having none at all. The size of swap allocated to files, or partitions, is usually chosen arbitrarily with many people adopting the "double the memory size" rule of thumb. Using a dynamic system can ease the maintainance of this size.

The relatively unknown dphys-swapfile package contains a simple script which will create and activate a swapfile at boottime which is sized appropriately for your system.

The advantage of this dynamic creation is that the swap will be resized automatically if you upgrade your memory and don't remember to do it.

Upon recent kernels there doesn't appear to be a significant penalty to using swap files as opposed to swap partitions. With this in mind I'd recommend files rather than partitions, to give yourself more flexibility.

To get started first remove any existing swap you have allocated. You can view any swap space which is in use by running:

skx@vain:~$ /sbin/swapon  -s
Filename                                Type            Size    Used    Priority
/dev/md1                                partition       2931768 557428  -1

Here we see that there is swap allocated to the physical raid volume /dev/md1. We can disable that by running:

root@vain:~# /sbin/swapoff /dev/md1

Once it is gone we can now install the package upon installation the system will create and activate then new swap:

root@vain:~# apt-get install dphys-swapfile
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed
  dphys-swapfile
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 9572B of archives.
After unpacking 111kB of additional disk space will be used.
Get: 1 http://apt-cache sid/main dphys-swapfile 20061020-1 [9572B]
Fetched 9572B in 0s (60.5kB/s)       
Selecting previously deselected package dphys-swapfile.
(Reading database ... 116206 files and directories currently installed.)
Unpacking dphys-swapfile (from .../dphys-swapfile_20061020-1_all.deb) ...
Setting up dphys-swapfile (20061020-1) ...
Starting dphys-swapfile swapfile setup ...
computing size, want /var/swap=1876MByte, generating swapfile ... of 1876MBytes
done.

Now whenever you boot you'll have /var/swap created at a size of twice your amount of physical memory, automatically.

You can verify this yourself with the swapon command we demonstrated earlier:

skx@vain:~$ /sbin/swapon  -s
Filename                                Type            Size    Used    Priority
/var/swap                               file            1921016 0       -3

If you wish to change the location, or size, of the generated swapfile please create the file /etc/dphys-swapfile and give it contents such as this:

# /etc/dphys-swapfile - user settings for dphys-swapfile package
# author Neil Franklin, last modification 2006.09.15
# copyright ETH Zuerich Physics Departement
#   use under either modified/non-advertising BSD or GPL license
# this file is sourced with . so full normal sh syntax applies

# where we want the swapfile to be, this is the default
CONF_SWAPFILE=/swap.file

# size we want to force it to be, default (empty) gives 2*RAM
CONF_SWAPSIZE=2048

##
#  Give yourself three times the memory size of swap?
#
# mem=$(grep MemTotal /proc/meminfo |awk '{print $2}')
# CONF_SWAPSIZE=$(expr $mem \* 3)
#

This article can be found online at the Debian Administration website at the following bookmarkable URL:

This article is copyright 2007 Steve - please ask for permission to republish or translate.