Swap partitions: what are they? Do I need them?
One of the most asked question by Linux beginners is: “What is this swap partition?” usually followed by “Do I need it?”. In this article I try to explain what these partitions are and if you need them or not.
What is RAM?
Before looking at what a swap partition is, we have to look at what RAM is. RAM is the acronym for Random Access Memory: an electrical component that can hold data. RAM is quite useful in computing because:
- It is fast. You can write and retrieve data pretty fast, that’s why it is used by programs that need fast access to their own data.
- It doesn’t matter where the data physically is, the access time will be roughly the same. The access time is the time it takes to get to a certain portion of the memory.
- Although these two qualities are enough by themselves they come at a price: RAM is volatile. Without electricity RAM won’t retain data (and will lose it if electricity goes away for a second).
So far RAM is great, but have you ever though about what happens if you run out of RAM?
What is a swap partition?
Swap is an “extension” of RAM: its purpose is to hold data when RAM is full. This extension physically resides on a disk (usually a hard disk) and is sensibly slower than physical RAM. So why would we use a slower memory to avoid lack of available RAM? That’s because you don’t want RAM to run out. When the system runs out of RAM it starts to become clunky and starts to kill processes, this results in a slower operating system, instability, and even data loss (e.g. if you’re working on something and your process gets killed). By adding a swap partition the system creates a pool of virtual memory that can be used as if it was physical RAM. Also if you want to use hibernation, you will need a swap partition since hibernation copies the entire content of RAM on the disk. Nevertheless, the main use for swap partitions is the process called swapping.
When data is stored inside of RAM it is usually stored in sections called pages, when a program becomes inactive its pages are not used any more but are still in RAM. Then, another program asks for memory but there’s no RAM left. That’s where swapping happens: a number of unused pages is transferred in the swap space so that the program that is asking for RAM can use it without problems. The swapping process is usually preferred to roughly using swap as raw RAM since it yields better results.
RAM size and Swap size
There are many esoteric bits about this particular matter. Some gurus claim that a swap partition needs to be X size others Y. There’s no particular rule of thumb, or rather there are too many. As many system administrators out there, I have my own:
|RAM||Swap size||Swap for Hibernation|
|1-4GB||RAM*2 + 100MB||RAM*2|
|>15GB||4GB (not needed)||At least RAM size|
Of course these values are indicative and each real case is different. For example, if you have 16GB of RAM, and you will run heavy programs all at once, you will need swap regardless. Also if you run servers, you should gauge the swap size according to the workload. Red Hat has a pretty nice table about this topic (from which I took part of mine).
Latest posts by mark (see all)
- Fedora 31 new features: Python 2 EOL, 32bit dropped - 16 October 2019
- Ubuntu 19.10 new features: ‘Eoan Ermine’ is a calm one - 9 October 2019
- How to install NextCloud 17 on Ubuntu 16.04/18.04/18.10/19.04 - 2 October 2019