Swap

Swap is a space on a drive used by the operating system when RAM (random access memory) is full. When RAM no longer has enough space to store data from running applications, the system will move some data that is not used frequently from RAM to swap. This helps the operating system avoid "lack of memory" and maintain the operation of applications.

Swap can be set up as:

  1. Swap partition: A separate partition on a drive that is specially formatted just for swap.
  2. Swap file: A file located on the main partition (e.g. ext4) and acts as swap space.

Comparison: Swap by partition and Swap by file

Criteria Swap partition Swap file
Performance Slightly better on older (small) systems. Performance is comparable on modern systems.
Easy to manage size Hard to change (must resize partition). Easy to resize by editing files.
Configuration Requires pre-created partition. Can be created at any time on an existing partition.
Resource usage Occupies a fixed partition, cannot be used for other purposes. Only occupies space when created and activated.
Flexibility Not flexible. Flexible, easy to create/delete swap file.
Supports snapshots or backups Cannot be easily backed up (unless backing up the entire disk). Can be backed up to the same partition containing the swap file.

Main benefits of Swap by partition

  1. Separate from main data:

    • Since it is a separate partition, swap is not affected by the main file system (ext4, xfs, etc.).
    • If an error occurs on the operating system partition, swap still works normally.
  2. Increased stability on old systems:

    • Some old operating systems or hardware (like before Linux kernel 2.6) handle swap partitions better than swap files.
  3. Stable performance:

    • With swap partitions, data is always saved to a fixed location on the drive, which reduces fragmentation compared to swap files on the system partition.
  4. Better security:

    • Since swap partitions are not part of the main file system, they are less likely to be "scanned" or leaked by unrelated tools.

Main benefits of file swap

  1. Easy to manage:
  • Swap files are easy to create, delete and resize without editing disk partitions.

  • No need to reboot the system or use partitioning tools.

  1. Flexible space utilization:
  • You can create swap files on any partition with free space.
  1. Comparable performance on modern systems:
  • With current Linux kernels, the performance between file swap and partition swap is almost indistinguishable.

When to choose each?

  • Use swap partition:

  • When installing the system from scratch and want to separate swap from data partition.

  • If you use an old operating system, or need high and stable swap performance on old hardware.

  • Use swap file:

  • When you don't want to edit the partition or need to change the swap size frequently.

  • When the system is already installed and there is no swap partition before.

  • If you need the flexibility to create or delete swap in a short time.