Skip to content

SWAP - do I need it?

Introduction 🔄

Swap space was often marginalized. Many users and system administrators believe that completely disabling swap is not a bad practice, especially on servers with large amounts of RAM. Based on an excellent lecture and article by Chris Down about SWAP, I will present several assumptions.

Why is it important? 🤔

The main value of swap space is not simply extending available memory, but rather providing the operating system with greater flexibility in memory management. Here are the key points:

1. Distinguishing between active and inactive memory 🔍

Modern operating systems, including Linux, categorize memory pages as active or inactive. Inactive pages are those that haven't been used for a long time and are candidates for transfer to swap space. This allows the system to more efficiently use physical RAM for actively used data.

2. Preventing out-of-memory (OOM) ⚠️

Lack of swap space increases the risk of OOM situations when the system doesn't have enough memory to allocate for critical processes. The OOM Killer mechanism in Linux may then terminate random processes, often those that are most resource-intensive, but not necessarily the least important.

3. Improving memory management 💾

Swap enables:

  • 📊 Moving rarely used memory pages to disk
  • 📈 Increasing memory available for buffer and file cache
  • ⚡ More flexible memory allocation management

Common misconceptions about swap

There are several, and some are still used as arguments, especially outside the administrator community:

Myth 1: "Swap slows down the system"

It's not swap itself that's the problem, but excessive switching between RAM and disk (known as thrashing). Properly configured swap doesn't cause significant system slowdown as it only moves rarely used memory pages.

Myth 2: "On modern systems with lots of RAM, swap is unnecessary"

Even in systems with large amounts of physical memory, swap offers benefits related to memory page classification and prioritization. The system can better utilize available RAM by moving inactive data to swap space.

Myth 3: "Disabling swap increases performance"

In reality, completely disabling swap can: - Reduce memory available for buffer and file cache - Increase OOM occurrence frequency - Limit kernel flexibility in memory management

How it works

The following diagram illustrates how the operating system manages RAM and swap space, moving inactive memory pages between them depending on system load and configuration:

flowchart TB

    subgraph APPS["Applications"]
        direction LR
        A1[Application 1]
        A2[Application 2]
        A3[Application 3]
    end

    subgraph OS["Operating System"]
        direction TB
        RAM[("RAM")]
        SWAP[("SWAP")]

        subgraph MM["Memory Management"]
            direction LR
            MGR[Memory Manager]
            PS[Page Scheduler]
            VMM[VM Manager]
        end

        subgraph STR["Memory Strategies"]
            direction TB
            ACTIVE[Active Pages]
            INACTIVE[Inactive Pages]
            SWAPPINESS[vm.swappiness]
            PRESSURE[Memory Pressure]
        end
    end

    APPS --> MGR

    MGR <--> RAM
    MGR <--> VMM
    VMM <--> PS
    PS <--> SWAP

    ACTIVE <--> INACTIVE
    INACTIVE --> PS
    SWAPPINESS --> PS
    PRESSURE --> PS

    RAM <-.-> |"Inactive pages"| SWAP
    SWAP <-.-> |"Restored pages"| RAM

    %% Dark mode compatible colors
    classDef default fill:#2a3144,stroke:#8494c1,stroke-width:1px,color:#e1e7f5
    classDef app fill:#2c415c,stroke:#81a6cc,stroke-width:1px,color:#e1e7f5
    classDef appsBox fill:#243447,stroke:#6d84b0,stroke-width:1px,color:#e1e7f5
    classDef ram fill:#3c394c,stroke:#a99ec3,stroke-width:2px,color:#e1e7f5
    classDef swap fill:#2d4250,stroke:#83adc3,stroke-width:2px,color:#e1e7f5
    classDef manager fill:#30483c,stroke:#82b5a2,stroke-width:1px,color:#e1e7f5
    classDef strategies fill:#413c36,stroke:#bfaa91,stroke-width:1px,color:#e1e7f5

    class A1,A2,A3 app
    class APPS appsBox
    class RAM ram
    class SWAP swap
    class MGR,PS,VMM manager
    class STR,ACTIVE,INACTIVE,SWAPPINESS,PRESSURE strategies

Optimal configuration

A key parameter affecting swap behavior is vm.swappiness. This parameter controls how aggressively the kernel will move memory pages to swap space.

Swappiness recommendations 🎯

  • 🔄 Default value in most Linux distributions is 60
  • 🎛️ Lower values (10-30) are recommended for production servers
  • ⚠️ Very low values (0-1) can be used but don't completely disable swap

Danger

A value of 0 means that the system maximally avoids using swap
until it absolutely must (e.g., when RAM is almost completely full).

Swap space size 💽

Traditional recommendations suggested swap equal to 2x RAM size, but in modern systems with large amounts of memory, this isn't practical.

  • 🟡 Low RAM (< 4GB): 1.5-2x RAM size
  • 🟢 Medium RAM (4-16GB): 1x RAM size
  • 🔵 High RAM (> 16GB): 0.5x RAM size or less, but not zero

Conclusions 📝

Summarizing Chris Down's lecture and personal experience, swap space is a valuable tool in system memory management that shouldn't be completely disabled. Instead, it should be properly configured to achieve an optimal balance between performance and system stability.

In times when disks are getting faster (especially SSD and NVMe), the cost of using swap is lower than years ago, while the benefits of flexible memory management remain significant.

Source

The discussed lecture and article is available at https://chrisdown.name/2018/01/02/in-defence-of-swap.html, which contains more detailed analyses and recommendations.