WTF Is Swap?

You’re getting sick of new terminology and concepts aren’t you?  All you want to do is try Linux, but it just won’t cooperate.

The latest in a series of roadblocks is the concept of swapping.  You may have stumbled upon it through experimentation, or you may have read our article on partitioning.  Either way, you’re wondering what the hell it is.

I know exactly what that’s like.  I came from a Microsoft Windows background, where the operating system actively shelters users from details like this.  I was frustrated and confused.  But someone helped me understand it, and now I want to pay it forward.

So here we go… swapping, swap partitions, and swap files.

 

What is Swapping

The physical memory (RAM) in your computer holds data that is currently, or expected to be, needed by the operating system.  Its job is to speed up your computer.

Operating systems manage the storage and retrieval of data by dividing memory into small chunks, called pages.  Unfortunately, there are only so many pages, and everything on the computer wants some.

Swapping is when the operating system copies a page from memory to a space on a storage device, like your hard disk.  Swapping increases the effectiveness of physical memory by allowing the operating system to reserve it for frequently used data.

It is important for two reasons.  First, when a computer program starts up, it loads a lot of data into physical memory.  Some of that data is only needed to initialize the program and never needed again.  With swapping, your operating system can take this unused data and move it out of your precious physical memory allowing something more important to take its place.

The second reason is that the available physical memory isn’t always enough.  When your system requires more memory than is physically available, the operating system has to dump something out.  If no swapping space is available, the data is simply lost.  This dumping of data, due to memory shortage, is one potential cause of programs crashing.  By having a place for swapping, the operating system has other options besides losing data.

 

Swapping Sounds Great, What’s the Catch?

Physical memory is blazingly fast.  Storage devices, like hard disks, are agonizingly slow.  Under certain circumstances, physical memory is hundreds of thousands times faster.

If your computer used normal storage devices in place of physical memory, you would throw it out the nearest window.  It would be intolerably unresponsive.

Therefore, when data is swapped out of physical memory, to the storage device, that data becomes slow to retrieve.

If used to supplement an adequate amount of physical memory, swapping speeds up your computer.  But it is not a replacement for physical memory.

 

Swap Space

The space used for swapping is called “swap space.”  It probably took a creative team days to think that name up.

There are two ways to provide swap space for Linux.  The first is using a swap file.  A swap file is simply a large file in your directory tree.

The second method is using a dedicated swap partition.  This is created and treated like any other partition.  The only difference is it is marked as a swap space for the operating system.

 

Do I Want a Swap File or a Swap Partition?

With a modern Linux kernel, there is no appreciable difference in performance between a swap file and a swap partition.

The only difference is in the setup and the administrative flexibility.  Swap partitions are easy to set up if done during installation of a Linux distribution, while swap files are more challenging to set up.  But, swap files can easily be moved or manipulated while swap partitions are harder to manipulate.

I use swap partitions.  I derive no benefit from being able to manipulate swap files, and a partition is easier to set up.  If you would rather be a rebel and use a swap file, you are welcome to do so.  More power to you!

 

How Big Should My Swap Space Be?

A common rule of thumb espoused by Linux aficionados is to allocate twice as much swap space as you have physical memory.  If you’re computer has 6GB of physical memory, allocate 12GB of swap space. 8GB of physical memory, allocate 16GB of swap space.

Given the low-cost of high-capacity storage devices, that guideline is a decent way of making sure you have enough.  However, it is quite probable that you will never need that much swap space.   If you have a lot of physical memory (6GB or more) and aren’t going to do any memory intensive tasks (like editing massive photos with GIMP), you can safely bump swap space down.

I have 16GB of physical memory in my primary computer, I’m not much of a memory hog, and the idea of allocating 32GB of swap space is hard for me to swallow.  Because of this, I ignore the rule of thumb and use a 1GB swap partition.  It has never caused me problems.

So the answer to the question “how big should my swap space be” is “there is no absolute answer.”  If you want to play it safe and make sure you’ll never have problems, follow the general advice of “double the amount of physical memory.”  If you want to be more frugal with storage space, you can bump down the amount of physical memory, or follow one of the countless other rules of thumb.

 

 

What If I Don’t Allocate Any Swap Space?

The universe will come to an end.

Okay, so that isn’t true.  No universe ending consequences will occur.  In fact, there’s a good chance you won’t notice any difference.  As long as you have enough physical memory to hold every piece of data that must be in memory, swap space isn’t essential and offers minimal benefits.

However, if you don’t have any swap space, and you run out of physical memory, Linux is going to start killing off processes asking for memory.  This may mean one of your running programs will crash.  Or, in extreme cases, the computer could crash (this isn’t likely with modern Linux distributions).

 

If you don’t like the idea of Linux deciding to close your Doctor Who/Battlestar Galactica mashup fan fiction you’ve been poring over, you should have swap space.  It’s cheap insurance.

 

There is one feature for which Linux requires swap space… suspension to disk.  Sometimes called hibernation, suspension to disk is a way of saving your computer’s current state and entering a low power consumption mode.  It does this by copying all the pages from your physical memory to the swap space, and then turning your computer off.  This way, when the computer starts up next, those pages are merely loaded back into physical memory and you’re back where you were.

For this to work, you must have more swap space than physical memory.  If you don’t, the feature is disabled.

 

Final Thoughts

Swap space is both a failsafe, if you run out of physical memory, and a performance enhancer under certain conditions.

Unless you have a good reason not to, you should always allocate some swap space.  You can create a swap partition when installing Linux or after, or use a swap file.

There is no absolute rule for how much swap space to allocate.  If you want to hibernate or suspend to disk, you need more swap space than physical memory.  Double the amount of physical memory is a popular guideline.  But if you don’t care about hibernation and have lots of physical memory, a small swap space is not an issue.

 

Written By

John is a sailing instructor and mechanical engineering student who happens to be a computer geek. To find more information about John, visit his website or find him on social media by clicking on the icons below.

Facebook Twitter Google+ Website

Tags:

Join the Conversation!