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.
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.
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.
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 not for Linux
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.