Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Linux swap files saved my low RAM PC from?

· 5 min read · Home Tech

In this article
  1. What is a swap file, and how does it differ from a swap partition?
  2. How swap files prevent freezes and OOM kills
  3. How to create and enable a swap file
  4. How much swap should you use?
  5. Common problems and misconceptions
  6. Swap file vs. zram: which is better for low RAM?
  7. Costs and trade-offs
  8. Alternatives to swap files
  9. Frequently Asked Questions

Linux swap files saved my low RAM PC from freezing, crashing, and out-of-memory (OOM) killer terminations when physical memory ran out. By moving less-used data from RAM to a dedicated file on disk, the system keeps running—slower but stable—so you can close a browser tab or save your work instead of losing it.

What is a swap file, and how does it differ from a swap partition?

A swap file is a regular file on your filesystem that the Linux kernel uses as virtual memory. When RAM is full, the kernel copies inactive memory pages to the swap file, freeing physical memory for active processes. A swap partition is a dedicated disk section used for the same purpose. The file approach is more flexible: you can create, resize, or remove it without repartitioning your disk. Modern Linux distributions, including Ubuntu and Fedora, often use swap files by default because they are easier to manage.

How swap files prevent freezes and OOM kills

Without swap, a system with low RAM (e.g., 2–4 GB) hits a wall when memory demand exceeds physical capacity. The kernel's OOM killer then selects a process—often the largest, like a web browser—and terminates it abruptly. That causes lost work and system instability. With swap, the kernel can evict idle pages to disk, so the system keeps responding, albeit slowly. For example, on a 4 GB RAM laptop, opening 30 browser tabs might consume 6 GB of memory. Swap absorbs the overflow, and the system remains usable, though disk I/O becomes a bottleneck.

How to create and enable a swap file

  1. Check current swap: Run swapon --show or free -h to see if any swap is active.
  2. Create a swap file: Use sudo fallocate -l 2G /swapfile (or dd if fallocate is unavailable). Adjust the size to your needs.
  3. Set proper permissions: sudo chmod 600 /swapfile to prevent other users from reading it.
  4. Format as swap: sudo mkswap /swapfile.
  5. Enable it: sudo swapon /swapfile.
  6. Make it permanent: Add /swapfile none swap sw 0 0 to /etc/fstab.

How much swap should you use?

The old rule of thumb—twice your RAM—is outdated for modern systems. For a low-RAM PC (≤4 GB), a swap size equal to your RAM is a reasonable starting point. For systems with more RAM, 2–4 GB is often enough for hibernation support. If you use hibernation, swap must be at least as large as your RAM to store the memory image. You can also adjust the swappiness parameter (default 60) to control how aggressively the kernel uses swap. Lower values (e.g., 10) favor RAM, while higher values (e.g., 100) favor swap. On a low-RAM PC, a swappiness of 30–60 is typical.

Common problems and misconceptions

One misconception is that swap is a replacement for RAM—it isn't. Swap is much slower (disk vs. RAM), so heavy swapping can cause system lag. Another issue is using a swap file on a failing or full disk; if the disk runs out of space, the system may crash. Also, some users worry about SSD wear from swap writes. Modern SSDs handle this well, but you can reduce wear by lowering swappiness or using a dedicated partition with the discard option. A common problem is creating a swap file on a filesystem that doesn't support it (e.g., some network mounts). Always use a local filesystem like ext4 or btrfs.

Swap file vs. zram: which is better for low RAM?

Zram creates a compressed block device in RAM, effectively increasing available memory by compressing data. It's faster than disk swap because it avoids I/O. Many distributions enable zram by default for low-RAM systems. However, zram uses a portion of RAM for the compressed data, so it doesn't help if RAM is completely exhausted. A common approach is to use both: zram for speed and a swap file as a fallback. For a 4 GB RAM PC, enabling zram (say, 2 GB) and a 2 GB swap file provides a good balance.

Costs and trade-offs

  • Disk space: A swap file consumes disk space permanently. On a small SSD, this can be significant.
  • Performance: Swapping causes disk I/O, which is slower than RAM. Expect lag when memory pressure is high.
  • SSD wear: Frequent writes to a swap file can wear out an SSD, though modern drives have high endurance.
  • Security: A swap file may contain sensitive data (passwords, keys). Permissions (600) and encryption (e.g., LUKS) mitigate this.

Alternatives to swap files

  • Swap partition: Traditional, but less flexible.
  • Zram: Compressed RAM swap, faster but limited by RAM.
  • Zswap: A compressed cache in RAM that fronts a swap device; it reduces I/O.
  • EarlyOOM: A daemon that kills processes before the kernel's OOM killer, potentially saving the system from a freeze.
  • Reduce memory usage: Use lightweight browsers, disable unused services, or add more RAM if possible.

Frequently Asked Questions

Q: Is swap file safe for SSDs? Yes, modern SSDs handle swap writes well, but you can extend lifespan by lowering swappiness or using zram.

Q: Can I use a swap file on an NTFS or FAT32 partition? No, swap files require a Linux-native filesystem like ext4 or btrfs.

Q: How do I remove a swap file? Run sudo swapoff /swapfile, delete the file, and remove the fstab entry.

Q: Why does my system still freeze with swap? If swap is too small or the disk is slow, the system may still thrash. Increase swap size or use zram.

Q: Does swap file help with hibernation? Yes, but the swap file must be at least as large as your RAM for hibernation to work.

Q: What is the difference between swap and virtual memory? Virtual memory is the abstraction; swap is the backing store on disk. Virtual memory includes RAM and swap.