Skip to content

Swap Memory

Swap space is an overflow buffer on your physical storage (HDD/SSD) that acts as virtual memory.

The Logic: Physical RAM is extremely fast but limited in capacity. When RAM fills up, the Linux kernel’s Memory Management Unit (MMU) identifies “inactive pages” (memory blocks not recently used) and moves them to the Swap space on the disk. This frees up high-speed RAM for active processes. Because disk I/O is vastly slower than RAM I/O, heavy reliance on swap degrades system performance (a state known as “thrashing”). Swap prevents out-of-memory (OOM) kernel crashes but is not a replacement for physical RAM.

image.png

  • Swappiness is a kernel parameter (0 to 100) defining how aggressively the system moves memory pages to Swap versus dropping the filesystem cache.
    • Default: 60. A balanced approach.
    • Low Value (e.g., 10): The kernel aggressively keeps application data in RAM and prefers dropping file cache. Ideal for interactive desktop systems where low latency is critical.
    • High Value (e.g., 90): The kernel aggressively swaps out inactive memory to keep the file cache large. Ideal for batch-processing servers where high disk I/O throughput is more critical than instant memory access.
  • Commands
    • To Check - cat /proc/sys/vm/swappiness OR sudo sysctl vm.swappiness
    • To Change - sysctl vm.swappiness=20 → NOT RECOMMEND TO CHNAGE THE DEFAULT VALUE
      • Permanent: Add to /etc/sysctl.confvm.swappiness=20
Section titled “1. Adding Swap via LVM - Partition or Volume [Recommended]”

Use this when extending a dedicated swap partition backed by LVM.

  1. Create LV: sudo lvcreate VolGroup00 -n LogVol02 -L 2G

  2. Format as Swap: sudo mkswap /dev/VolGroup00/LogVol02

  3. Persist (fstab): Add /dev/VolGroup00/LogVol02 swap swap defaults 0 0 to /etc/fstab

  4. Reload Systemd: sudo systemctl daemon-reload

  5. Activate: sudo swapon -v /dev/VolGroup00/LogVol02

    image.png

Use this for quick, flexible swap addition without altering partitions.

  1. Allocate Space (e.g., 64MB): sudo dd if=/dev/zero of=/swapfile bs=1024 count=65536(Math: block size 1024 bytes * 65536 blocks = 64MB) OR bs=1M count=64

  2. Format as Swap: sudo mkswap /swapfile

  3. Secure File: sudo chmod 0600 /swapfile (Critical: Prevents non-root users from reading RAM data dumped to disk).

  4. Persist (fstab): Add /swapfile swap swap defaults 0 0 to /etc/fstab

  5. Reload Systemd: sudo systemctl daemon-reload

  6. Activate: sudo swapon /swapfile

    image.png

  1. Deactivate: sudo swapoff -v /swapfile

  2. Remove Persistence: Delete the /swapfile line from /etc/fstab

  3. Reload Systemd: sudo systemctl daemon-reload

  4. Delete File: sudo rm /swapfile

    image.png

    • There is NO Direct Way to Increase or Decrease the Swap size.. but we can remove old one and create a new file with Desired Configuration..

    • Eg: To increase from 2GB → 4GB:

      Terminal window
      swapoff /swapfile
      rm /swapfile
      fallocate-l 4G /swapfile
      chmod600 /swapfile
      mkswap /swapfile
      swapon /swapfile
FeatureSwap PartitionSwap File
ArchitectureA dedicated block device/partition on the disk.A dedicated file residing within an existing filesystem.
PerformanceHistorically faster (especially on HDDs due to contiguous placement reducing seek time).Virtually identical in modern kernels (2.6+) as the kernel maps disk blocks directly, bypassing filesystem cache.
FlexibilityRigid. Requires volume management (like LVM) or downtime to resize.Highly flexible. Can be created, resized, or destroyed instantly on any mounted filesystem.
RecommendationRed Hat recommends partitions for raw performance reliability.Preferred for administrative ease and rapid scaling.