You clone the repo. You type make. The lights on your Raspberry Pi blink frantically for three seconds. Then—silence. The OOM killer just executed your build process with zero mercy.
Welcome to the joy of compiling on low-RAM hardware. That $4/month VPS you’re proud of, the Pi in your garage, the ancient laptop gathering dust—they can compile things. They just need a little coaxing. Here’s how to stop losing the RAM wars.
Check What You’re Actually Working With
First, see where you stand:
free -hswapon --showReal talk: if you’ve got 512MB physical RAM and zero swap, you’re essentially using a toaster. A very frustrating toaster. Your kernel will start killing random processes before you even finish configuring ./configure.
Add More Swap (The Actual Solution)
Swap is your safety net. Yeah, it’s slower than RAM—disk I/O versus memory access—but slow and working beats fast and dead. For one-time compiles, disk swap is perfectly fine.
Create a 2GB swapfile:
sudo fallocate -l 2G /swapfilesudo chmod 600 /swapfilesudo mkswap /swapfilesudo swapon /swapfileVerify it’s active:
free -hTo make it permanent (survives reboot), add this line to /etc/fstab:
/swapfile none swap sw 0 0Done. You’ve just given yourself 2 extra gigs of breathing room. Is it slow? Yes. Is it better than watching your build die at 47%? Also yes.
Tune vm.swappiness (Avoid the Swap Thrashing)
By default, Linux aggressively swaps—it’ll use swap even when RAM is available, which turns your compile into a grinding, disk-thrashing nightmare.
Lower vm.swappiness:
sudo sysctl vm.swappiness=10This tells the kernel: “Use swap only when you absolutely have to.” Your 2 AM self building Rust on a Pi will appreciate the responsiveness.
Make it permanent:
echo "vm.swappiness=10" | sudo tee -a /etc/sysctl.confLimit Parallel Build Jobs
Here’s the thing: each compiler process consumes RAM. Spawn 8 in parallel, and you’ve just multiplied your memory pressure. Don’t do that.
For make:
make -j2Or even -j1 if you’re really tight. Yes, it’ll be slower. But it’ll finish.
For Cargo (Rust):
CARGO_BUILD_JOBS=1 cargo build --releaseFor other build systems, check their docs—most have a jobs flag. The overhead is negligible compared to actually completing the build instead of getting OOM-killed at the finish line.
Use ccache (Cache Those Objects)
If you’re iterating on a build—tweaking a config, fixing a compile error, rebuilding—ccache is a lifesaver. It caches compiled object files so you never recompile the same source twice.
Install it:
sudo apt install ccacheTell your compiler to use it:
CC="ccache gcc" make -j2Or for C++:
CC="ccache gcc" CXX="ccache g++" make -j2First build is normal speed. Second build? Dramatically faster. On iterative development, this cuts your total time in half or better.
tmpfs for /tmp (Conditional)
Some build systems dump temporary objects into /tmp. If you’ve got at least 1GB of spare RAM after swap setup, you can mount /tmp as tmpfs for a speed boost:
sudo mount -t tmpfs -o size=1G tmpfs /tmpMake it permanent in /etc/fstab:
tmpfs /tmp tmpfs defaults,size=1G 0 0Fair warning: this burns RAM that could otherwise be used for the actual compile. Only worth it if you’re RAM-comfortable and you know your build thrashes /tmp a lot. Test it first.
Bonus: zram (Compressed Swap in RAM)
Ubuntu and Debian have zram-config, which compresses swap in RAM instead of writing to disk. It’s slower than pure RAM but faster than disk swap. One-liner:
sudo apt install zram-configRestart and you’re done. Great for Pis where disk speed is painful anyway.
Putting It All Together
Your low-RAM compile command, all tuned:
CARGO_BUILD_JOBS=1 CC="ccache gcc" CXX="ccache g++" make -j2or
CARGO_BUILD_JOBS=1 cargo build --releaseWill it be slow? Absolutely. The laws of physics haven’t changed. But your 512MB VPS or your overclocked Pi will now compile that project instead of crashing halfway through.
Now go compile your thing. It’ll be slow, but it’ll finish. And your 2 AM self will be grateful.