Swap in Linux is similar to virtual memory in Windows, which means that when there is a shortage of memory, a portion of the hard disk space can be used as virtual memory, thus solving the problem of insufficient memory capacity.
When installing a Linux system you need to set up a separate swap partition while Windows installation does not have this requirement, mainly because of the different ways of implementation.
- Windows virtual memory is dynamically allocated on the C drive (memory is used even when it is not in play)
- Linux virtual memory requires a separate swap partition (used only when memory runs out or when it goes into hibernation)
In terms of process, Windows is more convenient and the size is dynamic, but there are also problems, such as virtual memory swap files mixed inside the system partition, file fragmentation problems and capacity changes can affect the efficiency of swap files. Linux, on the other hand, is a separate partition, so there are no problems with file fragmentation and capacity changes.
In terms of principle, it is possible to install Linux without allocating a swap partition, especially if the computer has enough memory. However, it is recommended to allocate the swap partition to prevent unexpected situations or to make the computer support hibernation (hibernation).
Hibernation, after switching the system to this mode, the system will automatically dump all the data in memory to a hibernation file on the hard disk, and then cut off power to all devices. This way, when it’s time to resume, the system will read the contents of the hibernate file from the hard disk directly into memory and restore it to its pre-hibernate state. This mode consumes no power at all, so there is no fear of power supply abnormalities after hibernation, but at the cost of requiring a hard drive space the same size as the physical memory. However, because this method is completely powerless and preserves the working state, it is often used by notebook users as an alternative to shutting down the computer.
How to determine the size of Swap partition under Linux? Read along for expert advice.
From official RedHat advice.
|Amount of RAM in the system||Recommended swap space||Recommended swap space if allowing for hibernation|
|less than 2 GB||2 times the amount of RAM||3 times the amount of RAM|
|2 GB – 8 GB||Equal to the amount of RAM||2 times the amount of RAM|
|8 GB – 64 GB||4GB to 0.5 times the amount of RAM||1.5 times the amount of RAM|
|more than 64 GB||workload dependent (at least 4GB)||hibernation not recommended|
At the border between each range listed above (for example, a system with 2 GB, 8 GB, or 64 GB of system RAM), discretion can be exercised with regard to chosen swap space and hibernation support. If your system resources allow for it, increasing the swap space can lead to better performance.
You can see that Ubuntu gives very detailed advice, so we recommend following this to set up