Table of contents of the article:
What resonates in computer circles are the multiple opinions on the use of the Swap. Various points of view can be found: from those who follow the traditional idea, according to which Swap "must be double the RAM", to those who affirm that "today it is no longer necessary, given that the RAM we have is more than enough". At the same time, there are arguments that a "swap-free" system is faster, while others argue the opposite. With RAM becoming more affordable in 2015, the question is whether it's still worth reserving disk space for swap. The quick answer is: Yes, it's worth it. To better understand, let's analyze the situation in detail.
Virtual Memory: An Introduction
During my university studies, I had the opportunity to take two courses with prof. Montesi of the University of Bologna (he also taught in Camerino) referring to Unix systems and the text of Tanenbaum. Since then, the subject has become a deep passion of mine.
Every computing device is equipped with RAM, a fast-access memory that houses the data in use. It is very fast, much faster than any hard disk (even modern SSDs), and represents one of the main components that determine the operating speed of a machine, be it a server, a traditional computer, a smartphone, a tablet, etc.
Virtual memory is the sum of RAM and space that we define as Swap, which is space on a slower storage device that is used when the system needs to free up a certain amount of fast access memory (RAM).
The operating system decides what to keep ready for use and what to 'park' in a place that is less accessible, but still available. The total memory seen by the operating system is therefore the sum of RAM and Swap. Exceeding this limit will cause the system to generate OOM (Out Of Memory) messages and to manage the situation depending on the operating system in use.
Swap: Features and Management
Swap management varies between operating systems, but the basic concept remains:
Swap is space on a slower device that is used to extend RAM memory
Historically, Windows managed Swap through a file in the main file system. Its size is dynamic and grows as needs increase, it is mainly used when the machine's RAM begins to be full.
In Unix-Like systems, which include operating system families such as *BSD and GNU/Linux, Swap is usually allocated in a dedicated partition, designed and optimized for this specific purpose. This architectural choice is based on a fundamental principle: file systems are designed to manage files.
This includes implementing access permissions, tracking creation and modification times, data integrity management, and other features beyond simply reading and writing data to the device.
Adding these complexities to Swap could not only slow down the memory management process, but also add weight to the file system by making it more complex. Therefore, to avoid these problems, the Swap partition is dedicated exclusively to Swap, freeing the operating system from having to perform all these additional checks and processes.
The Swap-File option on Unix-Like systems
Despite the tradition of using dedicated partitions for swap in Unix-Like systems, there is also the possibility of using a swap file. This alternative can be particularly convenient in certain circumstances or when more flexible memory management is required.
Swap-files, unlike Swap-partitions, are special files within the file system that can be used to temporarily store data from RAM. This option offers some flexibility as the size of a swap-file can be changed dynamically, unlike a swap-partition which has a fixed size once created.
To facilitate swap-file management, several programs have been developed. These tools allow you to dynamically manage swap-files, creating them, increasing or decreasing their size, or removing them completely, according to the real needs of the system. This flexibility can be especially useful in scenarios where system memory needs can vary widely, such as servers with variable workloads or systems with limited storage resources.
Need to Swap in the Presence of Large RAM
A question that often arises regarding the use of the Swap is the following:
If I have a lot of RAM, much more than I need, why should I create swap space anyway?
This question is logically valid and makes sense, but the answer requires a deeper understanding of how Operating Systems, such as GNU/Linux, manage memory.
There are two basic reasons why you should use Swap, even when you have a lot of RAM:
1. Optimized RAM management
First of all, Operating Systems like GNU/Linux manage RAM very intelligently, trying to make the most of it. There is no such thing as "useless" or "unused" RAM: any free space is used to cache reads and writes to disks, making I/O operations faster and more responsive.
From this point of view, Swap becomes useful because it allows you to free up RAM from applications that have not been used for a certain period of time. These are "swapped", i.e. their data in RAM is moved to Swap. This operation frees up space in RAM which can be used to increase the disk cache or to handle any load peaks. The intensity of this is governed by the swappiness setting, a kernel parameter that decides how much the system should try to use Swap relative to RAM.
2. Hibernation support
Secondly, almost all GNU/Linux distributions use Swap as data storage space in case of hibernation. During hibernation, the contents of RAM are "frozen" and copied to Swap. This allows the system to shut down completely, saving energy, but to pick up exactly where it left off the next time it starts up. To support this feature, the Swap should be at least as large as the RAM itself, to ensure that all the contents of the RAM can be saved.
In many cases, it is possible to operate even without Swap, but its presence can improve resource management efficiency, system responsiveness and the flexibility of functions such as hibernation. Therefore, even if you have a large amount of RAM, creating some swap space is often a good practice.
Swap Partition Sizing
The decision on the size to assign to the Swap partition is one of the most discussed aspects when it comes to configuring operating systems. In fact, there is no definitive answer to this question, as it varies according to the specific needs of each system and the amount of RAM available.
There was a time when it was recommended that the Swap partition should be twice the size of RAM. However, those were the years when computer memory was measured in Megabytes and not in Gigabytes as is the case today. In that context, doubling RAM was a common practice to make sure there was enough virtual memory available for system operations.
In the modern world, where RAM is measured in Gigabytes, and you often have very generous amounts of RAM, this rule is no longer so relevant. For systems that need to hibernate, a suggestion might be to give the Swap a slightly larger size than the amount of RAM available. This is because, as mentioned before, the contents of RAM are copied to Swap during hibernation.
For systems that do not need this feature, such as servers, the size of the Swap can be evaluated on a case-by-case basis. However, it is generally recommended to always reserve at least a couple of GB to handle any peak loads. If there was a sudden increase in memory requirements and it was not possible to change the partitions, one could always resort to using a swap-file, which can be fixed or dynamically allocated as needed.
An important aspect to consider is data security. Portions of RAM memory are stored in the Swap, which means that it is possible to analyze the space even after the computer is turned off and potentially find sensitive data such as passwords or personal information. For those who are especially concerned about data security, there is the option of encrypting the Swap partition. This can be done either with a fixed key, which is requested at every system startup, or with a dynamically generated key at every startup, which becomes invalid when the machine is turned off, thus increasing the level of data protection.