To swap or not to swap

Swap space or swapfile use or no-use seems to be among the most common controversial issues in linux. If you wonder why would we engage in such a controversial topic we can explain. Most people using linux today who have relatively new multicore 64bit machines have huge disk drives and more ram than they can possibly use (except for video editors and game players). Most people who get on the hunt for lighter, simpler, easier to control and configure systems tend to end up searching among systems that don’t use systemd. So smart users with older cheaper equipment enjoy sometimes better computing performance than many who have the latest and hottest machine with a mediocre system. So using or not using swap is sometimes a choice between hunting for more RAM or more disk space use. So we thought we should take a shot and help out on this dilemma.

You should be able to make an educated choice and not base it on common assumptions and folklore. A swap is not something like a mousepad where it doesn’t matter whether you have a pretty one or you are using a piece of cardboard or nothing at all.

An important technical term that comes to mind first is “Overcommitment”, which means that processes can request more memory from the kernel than it is available physically (RAM) and as swap. Undercommitment is never a problem. The simplistic way to think of swap is that it extends your RAM in case it will not be enough. But that is half or less of the story.

RAM memory is very fast, in multiples of what a hard disk is. So using swap (disk written memory) is much slower than RAM. Even SSD (solid state disks) are much slower than RAM,although much faster than hard disks.

The obvious question is why would someone with “too much” RAM use swap?

– 1 You may need to use the hibernate function on your computer and in almost all cases we have read about, hibernation needs swap memory to write the current session so you can get everything restored when you turn the system on again.

– 2 Rarely used pages of memory after a significant up time will be moved to swap leaving more of the fast RAM available for other processes.

– 3 Some systems need swap for the “suspend” function, but not all. Some can maintain enough current to keep RAM alive for when the suspension period is over, not all though! And that system will wear out your battery charge even though it appears to be off!

– 4 During a power failure or a bug freezing the system swap memory can be utilized to reconstruct the file system so it will not be corrupted and destroyed. It is a safety measure that should not be overlooked. Ext3 and ext4 are journaling file systems, which means they keep notes of the changes in the filesystem while it is constantly changing during run time.

The next question would then be should one use a swap partition or a swapfile?

There was a time in the past that you couldn’t use a swapfile and everyone in unix/linux systems used a swap partition. Then things changed and people forgot what it is like to have a swap partition. On the one hand it is easier to resize a swapfile while it is time consuming to dismount, resize, and remount a swap partition. But a swapfile is written, erased and rewritten within the filesystem that holds your data, a swap partition is acting as a separate disk allowing better stability in your root filesystem. Also while doing backups of your system why waste space and time to be backing up a swapfile (unless you remember to exclude it). Also, we believe and this may change, if you are using btrfs you may not be able to use a swapfile. Do your research on this and keep up with development.

The third common question is also a source of controversy. How much swap do I need?

That is a more specific to use question. If you have 2GB of RAM but the system has never complained about you running out of memory then 512MB is adequate. If you have 4GB and you have monitored your system and see swap being constantly used, then 2GB may be a good choice. In most cases the conservative figure is 20% of RAM, the liberal figure is between 50-100% of RAM. Does this mean that someone with 16GB should use 8 or more GBs of swap? No, this is an overkill. Should someone with 1GB use less than 1GB? Unless disk space is in real shortage then it is ok, but half a GB should be right. On the other extreme someone with 1GB or less using 4GB of swap will end up in a really slow system trying to read and write memory to disk all the time.

What happens when both RAM and swap have been exhausted?

The system either crashes, gets very very slow trying to kill the most unnecessary processes that utilize memory, or there is nothing you can do other than hit the power button and reboot it. This is when your filesystem is at risk of corruption.

KDE and some other desktops these days have managed to always save your session for next boot, whether you go under hibernation or rebooting. Not all desktops do or can do this.

How do you create a swap partition?

If you use gparted it is relatively easy. You chop a piece of an existing partition or a piece of unused diskspace, create a new partition, and format it as linux-swap. Let’s say it is on drive sdc and it is partition 23, that is /dev/sdc23. (we will use sdc23 as an example throughout)

Check with the badblocks command that the partition has no bad blocks (if using gparted just select check on the partition and hit apply).

# sudo badblocks -v /dev/sdc23

If no errors use gparted to turn swap on

or use the command after you are done with gparted

# sudo swapon /dev/sdc23

To rebuild a damaged swap partition.

# sudo swapoff /dev/sdc23

# sudo mke2fs -c /dev/sdc23

If no errors are reported

# sudo mkswap /dev/sdc23

# sudo swapon /dev/sdc23

To create a 512MB swapfile in your root directory use this command:

# sudo dd if=/dev/zero of=/swapfile bs=1024 count=512k

bs is the block size count is the number of times /dev/zero is copied into /swapfile

# sudo mkswap /swapfile

# sudo swapon /swapfile

You should add either /dev/sdc23 or /swapfile to your fstab with an entry as

/swapfile none swap defaults 0 0

/dev/sdc23 none swap defaults 0 0

You could also use blkid /dev/sdc23 to get the UUID of the partition and we highly recommend using the uuid instead of the /dev/sdc23

UUID=123456-kdk-339-9d9d9g9 none swap defaults 0 0

If there is some shifting around on your partition table and swap partition becomes /dev/sdc22 while /dev/sdc23 is a partition holding date you need, the UUID will not change while the /dev/sdc23 option may result in loss of data.

Do check on your specific system’s wiki what the required format is as it varies from linux to linux.

Also some init systems like s6 require that you enable swap on their corresponding .conf file ( /etc/s6/s6.conf)

Feel free to comment, ask, add, correct, any of the above information. In general we think the gain of not using swap is minimal while the risk is high. Unless we hear otherwise, we suggest something between 25-50% RAM/swap ratio is best.

One thought on “To swap or not to swap

  1. well this is great, i didnt know about the badblocks command or about point #4. thats useful stuff.

    i didnt know there was controversy about swap. ive usually got a swap partition, i dont need it most of the time, i use some old machines that definitely require it but 2gb isnt a lot of disk space, 2gb is more swap than you need on some of the old machines i use (that i do need swap for.)

    ive always considered this a piece of cake– use it if you think you might run out of ram and crash without it, otherwise dont unless you want to. but point #4 is cool, i was not aware of that.

    Like

If your comment is considered off-topic a new topic will be created with your comment to continue a different discussion. This community is based on open and free communication, meaning we must all respect all in minimizing the exercise of freedom to disrupt such communication. Feel free to post what you think but keep in mind the subject matter discussed. It is just as easy to start a new topic as it is to dilute the content of an existing discussion.

This site uses Akismet to reduce spam. Learn how your comment data is processed.