The swap device – you probably have one, and you may have used it, but you may not know precisely what it is or whether you actually need it. Once upon a time, when a typical computer had only a few hundred megabytes of RAM, swap devices were critical to a machine’s performance and stability. But are swap devices still necessary? What exactly do they do?
This article will explain the purpose of the swap device, clear up a few common misconceptions, and help you understand whether or not you truly need one. Once you finish reading this article, be sure to check out my howto manage a swap device tutorial.
The most common misconception regarding swap devices is that it acts as RAM for your system, in addition to the physical DRAM that you actually have installed. While that is somewhat true, it’s not entirely accurate. Put simply, swap is a file or partition on your hard disk that is a place for your operating system to store memory contents that are not being actively used. By moving memory contents from RAM to swap, the operating system frees up real, physical RAM capacity, enabling it to store other, more active data in that RAM. The act of moving data from RAM to swap is known as “swapping out” in Linux (“paging out” in Windows). If the data in swap is later requested, it will be read from swap into RAM, which is referred to as “swapping in.” While swapping isn’t an inherently bad thing, it will cause poor performance in many cases.
To understand why “swapping” can lead to performance problems, it’s necessary to consider the relative performance of RAM and hard disk. For random operations
In fact, the difference between disk and RAM is so significant thay machines that are in the state where swap is being heavily and actively used as will often appear to be crashed or hung. So if swap is so slow, why do we have it? What’s it good for?
The true purpose of swap space isn’t to act as additional RAM; it’s to act as a storage location for things that are in RAM but aren’t actually being used. When you open a program, that program’s data (the executable, libraries, and all the files it needs to operate) must be stored in RAM. All open programs must be stored in RAM, regardless of whether or not they are actually being used; if you open Firefox and keep it minimized for weeks, it must still keep itself in RAM, even though it is idle. On systems with an abundance of RAM, this isn’t a problem, but when memory is a scarce resource (as it was several years ago), that RAM is essentially being wasted. This is where the idea of swap space comes in; the operating system can detect that a program is not being used, and move its memory contents to swap, freeing up RAM for other programs.
In the event that the swapped-out program is used, the operating system will read the data from swap and put it back in RAM. This is why you may notice that a program that’s been idle for a long time takes a while to become responsive.
There’s another beneficial aspect to swapping out programs that aren’t in use. In addition to storing program data, the operating system will generally use any available RAM as a cache for the hard disk, which greatly speeds up file access (remember that RAM is much faster than disk). By freeing up memory via swap operations, the amount of memory which can be used for disk caching is increased, and overall performance improves.
Modern computers can have a huge amount of RAM, so is swap still necessary? The answer is, as usual, “it depends.” Swap still plays the same role that it did previously (as described above), and in some cases it can be useful. Generally, these are desktop use cases, where applications may be left open for a long period of time without being used. My general recommendation for desktops is to have a swap partition, but ignore the old rule of thumb which said “your swap size should be twice your RAM size.” That simply doesn’t make sense anymore, when you might have sixteen gigabytes of DRAM in your system. For most users, even ten gigabytes of swap space is more than sufficient. You will most likely never use it all, but hard disk space is generally inexpensive, so it’s not worth the headache of trying to identify the optimal swap capacity.
For server environments, especially where there are large amounts of RAM installed, swap can be detrimental for performance. Servers generally don’t launch applications and let them sit around idle; a web server will always be serving web data, a mail server will always be running a mail program, and servers should never have user applications open and idle in the background. A word of warning, however; in the event that some runaway application on your server starts consuming more RAM than you expect, the Out Of Memory (OOM) killer will be invoked and start killing off processes, in a desperate attempt to save the machine from dying. The same is true if you do have a swap partition; the difference is that the system will become slower and slower (as it swaps data around) before the OOM killer begins to take action.
My general recommendation for server environments is to intelligently plan your memory usage – a server administrator should know how much RAM is needed by his services – such that your system memory is sized appropriately, and to use a minimally-sized swap partition. One additional improvement you can make is to tell the system not to use swap unless it is truly necessary; this is done by configuring the “swappiness” setting. Dedicated servers should run with low swappiness values; a value of ten is a good starting point, and in some cases a value of zero is optimal.
Finally, there’s a lot of talk about using Solid State Drives (SSD), or flash memory, as swap. Generally, this is inadvisable. The entire purpose of a swap device is that the system writes data to it that it doesn’t think it will need to access (read), making swap a write-intensive workload. SSDs are not ideal in cases where the primary operation is writing data, as write operations to SSD are far slower than read operations, and flash memory can only do a limited number of writes before it fails. This “write wear” problem is quickly going away with more modern SSDs, but ultimately SSD is relatively expensive when compared to hard disk, and using SSD for swap is a waste of fast, expensive storage.
I’d love to know how your system is configured. Do you have swap partitions on your desktops? How about your servers? Do you have so much RAM in your machines that you never use swap? Leave a comment below with your thoughts!