This post is about saving money on hobby projects, by scraping the bottom of the barrel with what your cloud provider offers.
Most cloud providers have a free tier of hosting, with low RAM. For example, Fly.io currently offers 3x instances of a shared-1-cpu instance with 256mb RAM. But what if you need more? Fly.io's pricing page shows you'd pay ~$10/mo for a 2gb RAM instance.
I have a side project that uses very bursty RAM, so it feels annoying to pay for 2gb RAM for a whole month, when I really only need it for a few minutes a day.
Enter the solution: swapping to disk.
Disk swap is when your OS uses available disk space as RAM (more info from the Arch wiki). Since RAM is faster than disk, this has a performance downside, but nowdays most cloud instances have fast SSDs. They'll still be about 10x slower than RAM, but if you're still reading this, you'll be too cheap to complain about the performance penalty.
Alright! Note that this won't work in Docker without privilege escalation, so you'll have to run this after your Dockerfile is built.
After running this, an application that would otherwise have 2GB of free disk space will use that disk space for memory allocation.
# Allocate 2048MB (2GiB) to a swapfile fallocate -l 2048M /swapfile chmod 0600 /swapfile mkswap /swapfile # Optionally modify swappiness (the likelihood to use swap) # (default is 60 on most Linux systems) # More info: https://unix.stackexchange.com/a/88820 echo 90 > /proc/sys/vm/swappiness # Enable the swap file swapon /swapfile
You'll want to call this from your application code, as your app starts up. As noted by a Fly maintainer, Fly automatically does this for Rails projects. This should prevent high/bursty-memory programs from being killed by the system OOM killer!
After running, you can run free —mega
to see if it's taken effect:
# free —mega total used free shared buff/cache available Mem: 231 75 95 0 60 148 Swap: 2147 65 2081
Now we see that we've got about 2gb of memory from swap thanks to our changes!