AWS
EC2
swap memory
cloud computing
tutorial

How do you add swap to an EC2 instance?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

Adding swap on an EC2 instance is a common way to reduce out-of-memory failures on small or bursty workloads. Swap is not a replacement for real RAM, but it can buy stability while you optimize memory usage or resize the instance. A good setup includes creation, persistence across reboot, verification, and realistic expectations about performance.

Know What Swap Is Good For

Swap is disk-backed virtual memory. When RAM fills up, the kernel can move less active memory pages to disk and keep more active pages in RAM.

That helps when memory pressure is occasional, for example:

  • build jobs
  • temporary spikes
  • low-memory utility instances

It is much less helpful when the workload is constantly short on RAM. Heavy sustained swapping usually means the instance is undersized or the application memory profile needs work.

Check Current Memory and Disk State

Before creating swap, inspect the instance state:

bash
free -h
sudo swapon --show
df -h /

You need enough free disk space for the swap file. Creating swap on an almost-full root volume can make the system less stable rather than more stable.

Create and Enable a Swap File

The most common pattern is a swap file. This example creates a two-gibibyte file:

bash
sudo fallocate -l 2G /swapfile

If fallocate is unavailable, use dd instead:

bash
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress

Then lock down the file permissions:

bash
sudo chmod 600 /swapfile

Mark the file as swap and enable it:

bash
sudo mkswap /swapfile
sudo swapon /swapfile

Verify the result:

bash
sudo swapon --show
free -h

Make the Swap File Persistent

Without an fstab entry, the swap file disappears after reboot. Add it explicitly:

bash
echo '/swapfile swap swap defaults 0 0' | sudo tee -a /etc/fstab

Then confirm that the line was written correctly:

bash
tail -n 5 /etc/fstab

It is worth checking this carefully. A bad fstab entry can create a different problem at boot time.

Tune Swappiness Carefully

Linux uses vm.swappiness to influence how aggressively it uses swap. On many application servers, a lower value is preferred so RAM is used more aggressively before the system swaps.

Inspect the current value:

bash
cat /proc/sys/vm/swappiness

Apply a common baseline:

bash
sudo sysctl vm.swappiness=10

Persist it:

bash
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

Treat these values as workload-specific settings, not as universal magic numbers.

Monitor Real Behavior After Enabling Swap

After adding swap, watch the instance for actual swapping behavior instead of assuming the problem is solved.

Useful commands include:

bash
vmstat 1 5
free -h
sudo swapon --show

If swap-in and swap-out activity stays high and application latency rises, the instance is probably underprovisioned. Swap helped prevent a crash, but it did not remove the root cause.

Remove or Resize Swap Later

If you no longer need the swap file, disable it cleanly:

bash
sudo swapoff /swapfile
sudo sed -i '\|/swapfile swap swap defaults 0 0|d' /etc/fstab
sudo rm /swapfile

To resize it, disable it first, recreate it at the new size, and enable it again. That keeps the state consistent and avoids partially applied changes.

Common Pitfalls

The biggest pitfall is treating swap as a permanent fix for chronic memory saturation. It is usually a stabilizer, not a capacity plan.

Another common issue is forgetting chmod 600, which leaves sensitive swapped data more exposed than it should be.

People also enable swap and forget the fstab entry, then discover after a reboot that the configuration was never persistent.

Summary

  • Swap on EC2 can reduce out-of-memory failures during short memory spikes.
  • Create a swap file, secure it with restrictive permissions, and enable it explicitly.
  • Add an fstab entry so the swap file survives reboot.
  • Tune swappiness only after measuring how the workload actually behaves.
  • If the instance keeps swapping heavily, optimize memory use or resize the instance instead of depending on swap long term.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.