1. Overview
Linux users are usually aware of swap space, but it is often a source of misunderstanding.
In this article, we’ll see some reasons why it’s always beneficial to have swap enabled. We’ll also address the common misconception that using swap slows our system down.
2. What Is Swap Space?
The swap space is located on disk, in the form of a partition or a file. Linux uses it to extend the memory available to processes, storing infrequently used pages there.
We usually configure swap space during the operating system installation. But, it can also be set afterward by using the mkswap and swapon commands.
Let’s see why swap space is useful during normal system operation, even when we have plenty of available physical memory.
3. When Does Linux Use the Swap Space?
3.1. Free Unused RAM for More Disk Buffers
Let’s start by using the free command to see how Linux uses memory:
$ free -h
total used free shared buff/cache available
Mem: 15G 15G 618M 17M 223M 711M
Swap: 46G 1.1G 45G
The columns we are interested in are free, buff/cache, and available.
In this example, we have 618M of free unused memory and 711M available to start new processes. The difference of 93M is currently part of the 223M of buff/cache and is being used for kernel buffers and page cache. Here, Linux estimates that if it’s needed, it can swap to disk up to that amount of RAM. The available memory is higher because we are using swap space.
Linux always tries to use RAM as much as possible. This is because it’s much faster than any other storage, even the fastest drives.
If a file is cached in memory, the associated process will run much faster. The action of caching files when accessing them the first time is called disk buffering. Writes are also made to memory and backed to disk soon after.
With more available memory, Linux can cache more files, potentially increasing its overall performance. A good way to free memory is to swap pages that are seldom used. This is one reason why enabling swap space is beneficial.
3.2. Suspend-to-Disk
The suspend-to-disk or hibernation functionality is useful on desktop systems. It saves the current system state from RAM to disk before shutting it down. The next time the system starts, the saved state will be restored to RAM, allowing us to continue where we left off.
Linux uses the swap space when suspending to disk. If we want to hibernate, we definitely need a swap partition or file the size of our RAM or bigger.
Now that we’ve seen the benefits of having swap enabled, let’s address the incorrect claim that swap slows the system down.
4. Swap Space Does Not Make a System Slow
Thrashing is a well-understood performance problem that we might wish to avoid. When RAM is almost exhausted, Linux can enter a cycle of swapping memory so often that it becomes the main activity. This makes the system unusable.
Some people try to avoid this situation by disabling swap altogether and using RAM only. Superficially, that might seem like good advice, but upon closer inspection, we’ll see that it’s not necessarily the case.
4.1. Freeing Memory Pages Will Always Happen
Even without swap space, some memory pages will still be swapped under certain circumstances.
As we’ve already learned, when a file is read, the data are put into the page cache to improve future access. Because the original file is on disk, it can still be swapped even if swap space is disabled.
4.2. Anonymous Pages Will Never Be Swapped
There’s another type of memory called anonymous memory. This memory is requested by processes but not backed by a file on disk. Among those anonymous pages, some are memory used only once during startup. Those pages are perfect candidates to be swapped. Thus, if we enable swap, they no longer waste physical memory.
5. Conclusion
In this article, we learned the reasons why swap space is recommended, regardless of the size of our memory. We also addressed the common misconception that swap space is detrimental to system performance.
It’s always good to have some space on disk reserved for swap space. Even if it does not noticeably increase performance, our Linux system will be healthier.