What happens if Linux runs out of RAM. Do I need a Swap file
November 20, 2021
Linux installers create a swap file or partition by default. The Swap file is used when the RAM is full, in this case the system writes part of the data that is not needed right now from the RAM to the Swap file, which somewhat unloads the RAM. Subsequently, the system can re-move the data from the Swap file into RAM.
In modern Linux, the swap file can be easily created, moved, deleted, and resized. That is, now it is not a disk partition, in order to delete which you must always carry out somewhat risky disk partitioning operations.
Nevertheless, I noticed that in recent years when installing Linux, I persistently switch to manual partitioning in order to partition a disk without Swap. The point here is not only to save disk space (although sometimes this is really important when installing an operating system on a USB flash drive), but also in the fact that, in my opinion, the Swap file is more harmful than useful. For some users at least.
My computer has 32 gigabytes of RAM. In my typical use, I have 2-3 virtual machines running, my Writer text editor open, and a web browser running with a lot of tabs open. I usually have 10-20 Gigabytes of RAM occupied with infrequent peak uses up to 31 Gigabytes.
As you can see in the screenshot, I have my swap file disabled.
The problem with the Swap is that when it is used, the system slows down quite. In fact, if you only have 4 Gigabytes of RAM then Swap might be useful for you. But in this case, you need to put up with the possible “freeze” of the operating system.
If there is a lot of RAM, then its full consumption is usually associated with a software error. For example, look at the following screenshot – due to a software error, the system has used up all the RAM.
The memory ran out, the system forcibly terminated the problematic process and continued working without crashing.
If the swap file were present, then it would also be completely full, and the process would also be terminated. At the same time, during the use of the swap file, the system would start to slow down greatly, perhaps it would become inaccessible.
That is, the result is the same, but the time for the “agony” of the system is shorter.
Of course, terminating the process due to insufficient RAM will result in the loss of unsaved data. Therefore, if you think that you can completely use up RAM, then you need Swap.
If your system regularly uses Swap, then you need to increase the size of the RAM – this will have a good effect on the performance of your computer.
- How to use the top command to monitor Linux processes (100%)
- htop command guide: how to view processes in Linux interactively (100%)
- How to use ps command to monitor Linux processes (100%)
- The in-memory file system - how to use tmpfs (82.2%)
- How to find out which process is causing high disk usage in Linux (67.9%)
- How to configure HTTP Digest Authentication in Squid (RANDOM - 50%)