
Survive memory crashes without upgrading your instance
THIS IS NOT CLICKBAIT, JUST ACTUAL STUFF
I was running a self-hosted GitHub Actions runner on a t3.micro (1 GB RAM) to build Flutter and Node based apps. Every single pipeline run crashed. CloudWatch showed the exact same pattern every time: CPU spike, memory spike, process dead. No graceful failure, no useful error, just a killed build.
The obvious fix was to upgrade to a t3.small and move on. Instead I enabled swap, and the crashes stopped completely. This post is a walkthrough of why that worked, how I set it up, and where swap is a genuinely bad idea instead of a clever one.
Why a 1 GB instance chokes on a build
Flutter and Node toolchains are not light. A
flutter build or an npm run build on a JS heavy project spins up multiple processes at once: the Dart/Flutter tool, Gradle if you are building Android, a Node process for bundling, sometimes a linter or test runner in parallel. Each of these wants its own chunk of memory, and on a 1 GB box there simply isn't enough to go around.When Linux runs out of physical memory, the kernel's OOM killer steps in and kills whichever process looks like the biggest offender. On a CI runner, that is usually the build process itself. So from the outside it looks like your pipeline randomly failed. From the inside, it is not random at all, it is memory pressure hitting a hard ceiling.
The instinctive fix is "just upgrade the instance." That works, but it also means a recurring cost increase for something that only needs extra headroom during short bursts, not all the time.
What swap actually does
Swap space is not extra RAM. It is disk space that the kernel uses as an overflow area for memory pages it does not need to keep in physical RAM right now.
When memory fills up, the kernel looks for pages that are idle or "cold" and moves them out to the swap file on disk. That frees up physical RAM for whatever is actively demanding it. When those swapped out pages are needed again, the kernel reads them back in.
On an EC2 instance, the swap file lives on your attached EBS volume. EBS is not as fast as RAM, not even close, but it is fast enough to absorb a short memory spike during a build without the process dying.
The important part: swap does not make your instance faster. It makes it survive. Those are different problems, and mixing them up is where people get burned.
Setting it up on Amazon Linux / Ubuntu EC2
This is the exact sequence I used. Adjust the swap file size to your instance and workload; 1 to 2 GB was enough for my Flutter and Node builds on a 1 GB RAM t3.micro.
# create a 2 GB swap filesudo fallocate -l 2G /swapfile
# lock down permissions, only root should read/write itsudo chmod 600 /swapfile
# format it as swapsudo mkswap /swapfile
# turn it onsudo swapon /swapfile
# confirm it is activesudo swapon --showfree -h
To make it survive a reboot, add it to
/etc/fstab :echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
If
fallocate is not supported by your filesystem (some older EBS + ext configurations), fall back to dd :sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
Tuning swappiness
By default, Linux is fairly eager to use swap even when there is still some RAM free, controlled by a kernel parameter called
swappiness (0 to 100). For a CI runner, I want the kernel to use RAM first and only fall back to swap when it is genuinely under pressure, not preemptively.# check current valuecat /proc/sys/vm/swappiness
# lower it, e.g. to 10sudo sysctl vm.swappiness=10
# make it permanentecho 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
A lower value keeps swap as a safety net for spikes rather than something the kernel reaches for constantly, which matters on EBS backed swap where every swap read and write is an I/O operation, not free.
Watching it work
During a build, I kept an eye on memory and swap usage in a second terminal:
watch -n 2 free -h
and cross checked against CloudWatch memory metrics (via the CloudWatch agent, since EC2 does not report memory usage by default). The pattern changed completely after enabling swap: the same CPU and memory spike still happened, but instead of the process getting killed, RAM usage plateaued near the physical limit while swap usage ticked up for a few seconds, then came back down once the build's peak memory moment passed.
The result
Builds stopped crashing. The pipeline that used to fail on essentially every run started completing reliably. And the cost of the fix was zero, since I was already paying for the attached EBS volume; I just used a slice of it differently. That kept me on the t3.micro instead of moving to a t3.small, saving roughly $7.50 a month, which is a small number on its own but adds up if you are running several small runners or side project instances.
Where swap is the wrong answer
This is the part people skip, and it matters more than the setup steps.
Swap is a burst absorber, not a performance upgrade. It is a reasonable fix when:
- The workload is short lived, like a CI build, a batch job, or a one-off script
- Occasional slowness during the memory spike is acceptable
- The process just needs to survive the peak, not run fast during it
It is the wrong tool when:
- You are running a live API, a database, or anything latency-sensitive. Swapping under a database workload turns query latency into a mess, since disk I/O is orders of magnitude slower than RAM access, and a database that starts swapping tends to degrade unpredictably rather than gracefully
- Memory pressure is constant rather than a brief spike, because then you are not absorbing a burst, you are permanently running on disk-backed memory, and performance will reflect that
- You need predictable, consistent response times
For anything in that second list, the right move is still to upgrade the instance, not to paper over it with swap.
Takeaway
Swap on EC2 is a legitimate, well-understood Linux mechanism, not a hack in the "please don't do this in production" sense. It is genuinely underused on small instances running CI runners, build agents, or batch workloads, where the failure mode is exactly what swap is built to absorb: a short memory spike that would otherwise kill the process outright.
Know the difference between "this process needs to survive a spike" and "this process needs consistent low latency," and pick accordingly. Getting that distinction right is the difference between a smart cost optimization and a slow, flaky production system.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article