Translation(s): English - Français - Italiano


About Swap Space

Swap space under Debian and other GNU/Linux based operating systems is a form of virtual memory. Simply put this means that if the system runs out of physical memory (RAM), then it will transfer some of the lesser used data in RAM to this space. Swap space is also fundamental to the processes of the "Suspend" and "Hibernate" features of Linux. Swap is primarily setup as a separate dedicated partition(s) (recommended) or as a specially created file(s) residing on an existing file system.

As of the Linux 2.4 kernel series, the Linux kernel can handle up to 32 separate swap partitions and or swap files at any given time. This is highly uncommon though, as hard drives are much slower than RAM. Most systems will have only swap partition, or one swap partition per instance of Linux. The most common time to setup swap is during the Debian installation process, but it can be created or modified at any time.

Note that there are kernel modules Zram and Zswap which allows compressing the swap inside RAM. Zram does not need any physical swap device.

The recommended amount of swap space has traditionally been double the amount of system memory. This has changed over time to one and half times system memory, both answers are decent baselines but are becoming less and less useful answers to the question as time passes. There are many variables about your system and intended use that will determine the available system swap you will want to have.

Things to consider when deciding how to setup swap space on your machine

  1. Do you intend to use the Hibernate and Suspend functions? If you intend to use either, you will need to ensure your available swap space is large enough to hold all information your running system will have in RAM. If you intend to use Hibernate you should ensure that your Debian installation has access to a swap partition or file that is NOT shared with another instance of GNU/Linux.
  2. How much memory does your machine have? If your machine has a large amount of RAM available to it and you don't need Hibernate or Suspend, then it will be safer to have less swap available to the system. If your machine has very little RAM available to it, such as the case in older machines and netbooks you should ensure to make a swap space that is large enough. Double the system memory would be a good suggestion in this case.
  3. How much memory will be taken by the applications you intend to run? The memory usage of a Debian GNU/Linux system will vary greatly on the applications you choose to run. For instance, running a lighter weight desktop environment such as Openbox or Xfce and their associated applications will use less memory than GNOME or KDE.
  4. How much hard disk do you have available? If using a small solid state disk or USB key for the installation of Debian this hard disk space might be at a premium. In such cases it might make more sense to install more system memory if needed than use a large swap space. This also has the benefit of speeding up the system, as the read/write speed of hard disks is much slower than RAM.

In the end, if your machine runs out of both RAM and swap space then the system will crash. This has lead to the simplistic but safe answer of use a swap space that is "double the amount of system memory".

Creating and modifying swap on an existing machine

Swap partitions can be created and modified using graphical tools such as GParted or command line tools such as parted or gnu-fdisk. Creation of swap files is handled by the dd(1) command. To create a swap file using dd:

dd if=/dev/zero of=/PATH/FILENAME bs=1024 count=SIZE
chmod 600 /PATH/FILENAME
mkswap /PATH/FILENAME
swapon /PATH/FILENAME

PATH is the exact path to the directory you wish to place your swap file, FILENAME is the name you wish to give your swapfile and SIZE is the size you wish your swap file to be in kilobytes (for example 1048576 is 1 gigabyte). The chmod command is to make sure the file is not world readable for security reasons.

Once you have created your swap partition or file you will need to add an entry in the /etc/fstab file of every instance of Linux that will be using that swap space.

Open the file /etc/fstab with your preferred text editor and root privileges.

If adding an entry for a swap partition

Run the command blkid with root privileges. This will show you the UUID of all partitions on your machine. Find the swap entry and copy the UUID and add it directly after the = in the entry you create in /etc/fstab. The # line is not needed but is considered good practice.

# Dedicated swap partition created on DATE
UUID=       none    swap    sw      0       0

If adding an entry for a swap file

The # line is not needed but is considered good practice.

# Swap file created on DATE
/PATH/FILENAME       none    swap    sw      0       0

Known issues and troubleshooting swap

You can verify that a particular swap partition or file is active and functioning on any GNU/Linux based system by trying to switch it on. This is done using the swapon(8) command with root privileges, for example swapon /dev/sda5 where sda5 is the actual name of the swap partition on your machine. You can look up the names of all partitions on your machine using the command cat /proc/partitions. Or, to check that a swap file is active run swapon /PATH/FILENAME. If that particular swap space is active you will get output similar to below.

root@debianhost:/# swapon /dev/sda5
swapon: /dev/sda5: swapon failed: Device or resource busy

If you do not get this output you should check the contents of /etc/fstab for errors. For swap files ensure the /PATH/FILENAME entry is correct. If using a swap partition lookup the UUID of the swap partition using the blkid(8) command. Simply run blkid with root privileges and compare the UUID it has with what is in /etc/fstab. If there is a discrepancy, replace the UUID in /etc/fstab with the one output by blkid.

Note

As of this writing with the Debian installer on the current stable images (version 6.0.1), the manual partitioning will default to formatting all found swap partitions on all disks. This will invalidate any UUID entry in the /etc/fstab files of any other installed GNU/Linux based system that is using those swap spaces.

In order for the installer not to format a swap partition, it must manually be marked "Do not use" during partitioning. The installer will format any swap partition not marked "Do not use" and use it for swap space on the installed system. If sharing the swap space with another instance of GNU/Linux you will need to update their /etc/fstab files manually. To do this simply run blkid with root privileges and put the UUID it outputs for the swap partition in place of the old UUID in the other Linux instance's /etc/fstab files.

For more information on virtual memory see Virtual memory.


CategorySystemAdministration