Swap

A virtual memory mechanism that moves inactive segments of RAM to secondary storage to free up physical memory for active processes.

Swap is a designated space on a storage drive—either a partition or a file—used by the operating system to extend the capacity of physical RAM. When physical memory becomes saturated, the kernel moves inactive memory pages to this disk space to accommodate active workloads.

How it works

The memory management unit handles the transition of data blocks. When a process requests data currently residing in swap, a page fault occurs, triggering the kernel to move that data back into RAM. Key technical considerations include:

  • Latency: Accessing swap on an NVMe SSD is significantly slower than RAM, while mechanical HDDs can cause system thrashing due to high seek times.
  • Stability: Swap acts as a safety net against Out-Of-Memory (OOM) errors, providing a buffer during memory-intensive operations.
  • Memory Reclaim: By offloading dormant application data, the OS can allocate more physical RAM to high-speed filesystem caches.

On modern Linux servers, administrators often use the free -m command to monitor usage and swapon to initialize swap files. A common practice is to set a small swap space (e.g., 2GB) even on high-RAM systems to ensure the kernel can efficiently manage memory pressure and process isolation.