Most Linux installations recommend that you include a SWAP partition. This may seem odd to Windows users, who are used to having their entire operating system on a single partition.
So what does a SWAP partition do, do you even need one, and how big should it be? These are all important questions that, with the right answers, can seriously improve your system’s performance.
Overflow from Memory
In it’s simplest sense, the SWAP partition acts as an overflow to your (RAM) memory. If your memory is filled up completely, any additional applications will be run off of the SWAP partition rather than memory.
This sounds like an easy way to increase the amount of usable memory without actually getting more RAM, but that isn’t the case. RAM is the ideal hardware for memory because it’s extremely quick, unlike hard drives which are, relatively speaking, extremely slow. The arrival of solid state drives have made the performance hit less of an issue with their much-improved speeds, but even they can’t match RAM – plus, you wouldn’t want to cause additional wear and tear on your solid state drive.
The closest analogy of the SWAP partition would be Windows’s pagefile, although there are many technical differences between the two.
A SWAP partition can also help move some items from your memory to your hard drive in order to leave more room in memory for more important items. This implies that items that are rarely ever touched would get moved to the SWAP partition.
The threshold of what is considered “rare” depends on the “swappiness” (yes, that’s the actual term used), which is configurable. A higher swappiness means that items are more likely to be moved to the SWAP partition; a lower swappiness means that items are less likely to be moved to the SWAP partition.
Lastly, a SWAP partition is used as the destination of your memory’s contents whenever you tell your system to hibernate. This means that without a SWAP partition, hibernation on Linux is impossible.
Of course, it’s actually quite rare for users to use the hibernation feature, so this may not matter to you.
Do You Need a SWAP Partition?
So, does this mean that a SWAP partition is necessary? Absolutely not! A Linux system can perform perfectly well without a SWAP partition. However, there are a few advantages and disadvantages of having one.
- Provides overflow space when your memory fills up completely
- Can move rarely-needed items away from your high-speed memory
- Allows you to hibernate
- Takes up space on your hard drive as SWAP partitions do not resize dynamically
- Can increase wear and tear to your hard drive
- Does not necessarily improve performance (see below)
When SWAP Partitions Don’t Help
What? SWAP partitions don’t always help improve performance? Let me explain a scenario where having a SWAP partition was actually worse than not having one.
I had Linux installed on a netbook that only had 1GB of memory and a 5400rpm hard drive. With only 1GB of memory, you can imagine that it can fill up pretty quickly with a few open browser tabs. The SWAP partition allowed me to keep them all open as the memory overflow simply went to it.
But then a bottleneck appeared, because of the hard drive’s 5400rpm speed. Because the hard drive was so slow, and the system constantly wanted to access the SWAP partition, the netbook became extremely, extremely sluggish to the point where it became virtually unusable unless I closed everything to free up some memory.
The set swappiness didn’t guarantee that, even though there was now space in the memory, everything in the SWAP partition would be moved back over. Instead, a lot of that would stay in the SWAP partition, causing the netbook to continue to be sluggish. This was only fixed by a reboot, which took a while anyway because the system had to remove everything from the SWAP partition before shutting down.
So, here’s what I would recommend:
- If you would like to be able to hibernate your computer, then you should have a SWAP partition. The size of this partition should be the size of your installed memory, plus an additional 10-25% to leave room for any items that were already moved over into the SWAP partition.
- If you just want a small performance boost (and you have at least a 7200rpm hard drive), then you can add a SWAP partition if you want, but it’s not needed unless you have less than 4GB of installed memory. The size of this can be whatever you’d like, but I wouldn’t make it any bigger than you would if you were creating a SWAP partition to enable hibernation.
- If you have a 5400rpm hard drive, then you shouldn’t create a SWAP partition simply because the bottleneck will make your computer worse off. However, if you absolutely want to have SWAP, then you can still create a partition using the same sizing guidelines outlined above – but change the swappiness value to something much lower.
To change the swappiness, you need to run the command
gksu gedit /etc/sysctl.conf which will launch a text editor named Gedit, a fantastic all-around text editor, for the configuration file we need to change. Next, locate “vm.swappiness” and change it to a different value (preferably 10). If you don’t see this parameter, add this line to the end of the file: vm.swappiness=10
The value you enter indicates when you want Linux to start actively moving processes from the memory to the SWAP partition. So for example, a value of 10 indicates that processes will be moved when memory usage reaches 90%; the default swappiness value in Ubuntu of 60 indicates that processes will be moved when memory usage reaches 40%.
There are a lot of other details that go into this, but they would only make things more confusing.
SWAP partitions can make a major difference in your system’s performance – sometimes for better and sometimes for worse. Now that you’re well-educated, you should be able to make the right decisions.
Looking for other ways to speed up your Linux system? Check out these four other quick and easy tips.
What have you heard about SWAP partitions? Do you think they’re worth using? Let us know in the comments!
Image Credits: Daniel Rocal