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.

From the official Ubuntu proposal.

        RAM   No hibernation    With Hibernation  Maximum
      256MB            256MB               512MB    512MB 
      512MB            512MB              1024MB   1024MB
     1024MB           1024MB              2048MB   2048MB

        RAM   No hibernation    With Hibernation  Maximum
        1GB              1GB                 2GB      2GB
        2GB              1GB                 3GB      4GB
        3GB              2GB                 5GB      6GB
        4GB              2GB                 6GB      8GB
        5GB              2GB                 7GB     10GB
        6GB              2GB                 8GB     12GB
        8GB              3GB                11GB     16GB
       12GB              3GB                15GB     24GB
       16GB              4GB                20GB     32GB
       24GB              5GB                29GB     48GB
       32GB              6GB                38GB     64GB
       64GB              8GB                72GB    128GB
      128GB             11GB               139GB    256GB
      256GB             16GB               272GB    512GB
      512GB             23GB               535GB      1TB
        1TB             32GB              1056GB      2TB
        2TB             46GB              2094GB      4TB
        4TB             64GB              4160GB      8TB
        8TB             91GB              8283GB     16TB

You can see that Ubuntu gives very detailed advice, so we recommend following this to set up