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.
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:
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:
If fallocate is unavailable, use dd instead:
Then lock down the file permissions:
Mark the file as swap and enable it:
Verify the result:
Make the Swap File Persistent
Without an fstab entry, the swap file disappears after reboot. Add it explicitly:
Then confirm that the line was written correctly:
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:
Apply a common baseline:
Persist it:
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:
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:
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
fstabentry 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
- How do you call the data model of DynamoDB and Cassandra?
- How do you comment out lines in AWS CLI config and credentials files?
- How do you create an EC2 instance with multiple key pairs?
- How do you delete an AWS CloudWatch metric?
- How do you delete an AWS ECS Task Definition?
- How do you delete an AWS EMR Cluster?
- How do you full text search an Amazon S3 bucket?
- How do you get kubectl to log in to an AWS EKS cluster?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.