I recently wrote about the Linux swap device and what it’s used for. This tutorial covers the basics of actually managing the swap devices and/or files on your system. Topics covered include how to enable and disable swap, how to create (and remove) additional swap space, and how to measure swap usage.
Swap space is managed using the appropriately named “swapon” and “swapoff” commands, while “swapon -s” prints a summary of the current swap devices and usage. (For those who are interested in going more in depth, “swapon -s” actually reads from /proc/swaps.) On my mostly-stock CentOS 6.4 system, the output looks like this:
You can see that /dev/dm-1, of size 4096992 bytes, has zero usage. There are a couple of interesting items in this output:
/dev/dm-1 is listed under the “filename” column, and the type is listed as “partition.” This output is important, because swap can be contained both on dedicated swap partitions, as well as on files that live inside regular filesystems (like your root partition). The fact that you have a partition under a filename column is acceptable, because remember that in Linux, everything is a file.
The swap device has no priority. Were we to be using multiple swap devices, we are able to specify the priority in which they are used (higher numbers indicating a higher priority). This may be useful if we’ve undersized our swap disk or if we’ve just added some fast storage, and we need to add some additional swap as a file. Priority for a device is specified when it is enabled using “swapon”, by using the “-p” flag followed by the device’s priority (e.g, “-p 3”).
Next, we disable the swap partition using “swapoff”:
Note: Do not disable a swap device on a production system, unless you are very certain that is what you want to do. If swap is heavily utilized, then turning off a swap device can cause a significant performance drop!
This section shows how to disable a swap device with the “swapoff” command. The usage is simple enough; you simply specify the file you wish to disable. As mentioned in the note of caution above, disabling a swap device that has some contents (put another way, you could say that the “Used” counter is greater than 0) will require the system to read those contents off the swap device and into RAM (or another swap device if necessary). After the “swapoff” commands complete, another “swapon -s” to check the status shows that it is no longer in use.
Finally, we can re-enable the same device for usage as swap again…
…and the system is restored to its original state. But enabling and disabling existing swap devices isn’t always too useful; we’ll probably want to create and destroy them at some point.
Remember that swap can exist either as a device or as a file inside a file system. If you are using an entire disk or existing partition as swap, you can skip the next couple of paragraphs. If you’re creating a swap partition, you would do that in the same way that you would create any other partition (for example, with “fdisk” or “parted”), except that we are going to mark it as swap instead of writing a filesystem to it. If you’re using a file to create additional swap space:
Figure out where you want to store the file. For this example, I’ll just create a small swapfile on my root partition.
Use the “dd” command to create an empty file of the desired size:
Note: For performance reasons, creating multiple swap locations on the same drive (e.g, a swap partition and a swap file – which will be the case on most single-disk setups) is not recommended. If you are doing it just as a test, be sure to disable and delete the swapfile when done.
In this command, I am creating a file called /swapfile of size 1GB (blocksize of 1MB multiplied by a count of 1024 blocks) that will be used as additional swap. Of course, this command is only necessary when you’re creating a file for use as swap; if you are using a disk, continue to the next step.
The “mkswap” command is used to mark a file or device as swap space. The basic usage is simply to call “mkswap [filename]”. Note that, just as when you create a filesystem on a device, the data on the device or in the file will be destroyed.
The warning message is letting us know that it is skipping over the first block of the device, just in case it contains information required to boot the system.
Finally, use the “swapon” and “swapoff” commands to enable or disable the swap devices and files:
Note the usage of “swapoff -a”; the “-a” flag tells the “swapoff” command to disable all swap devices.
Finally, it may be desired to add a swap device to the “/etc/fstab” file, for example if you want to have the swap device available at boot time. The fstab entry format is the same as with a standard disk:
For a swap partition, you should use the following values:
“device”: The device name or UUID of the swap file. UUIDs and labels are always preferred over device names, since device names may change on reboot if disks are added or removed.
“file|none”: Swap devices are not mounted anywhere, and therefore the value should be “none”. (Note: There is a discrepancy on my system where the man page indicates that the type should be “none”, but my fstab lists my swap device as “swap”. Apparently either works on CentOS 6.4.)
“filesystem”: Swap devices should use “swap” here.
“options”: Should be listed as “default”. If you wish to set a priority level, the syntax is “pri=[int]” where “[int]” is the priority (e.g, “pri=3”).
The dump frequency and fsck priority can both be either omitted or set to 0, as swap devices are neither dumped (a Linux backup program) or checked for integrity.
Once swap is configured the way you want it on your system, you may have occasion to review the swap usage. You may have noticed above that the “swapon -s” summary displays the current amount of swap used on a device. While this is certainly useful information, the “free” command provides a more interesting look at the system memory usage overall:
Since this system’s swap has been detached and reattached, and there’s no real memory pressure on the system, we see that no swap is used; in fact, there’s about 1.6GB of physical memory free. Note that the “-m” flag for “free” tells it to display values in megabytes (as a side note, if you ever watch someone who’s been around for a long time, they’ll tend to use “-k” with “free”, for kilobytes).
Managing swap space in Linux is fairly simple, consisting of only a few commands: “swapon”, “swapoff”, “mkswap”, and optionally a modification or two to “fstab”. While changing swap configuration is rare, swap usage is a key metric when diagnosing a poorly performing system – remember the “free” command for that purpose.
Finally, there is an ongoing debate as to whether or not it makes sense to continue using swap at all in modern systems that have such a large amount of RAM. A discussion on whether swap is necessary is available here.
I’d love to know whether or not you have a swap partition, and whether or not it’s actually used. Post a comment below with your “free -m” output, and see how your system measures up!