Boosting Your DigitalOcean Droplet's Memory: No Upgrade Needed When You're Short on RAM
February 29, 2024
The entry-level DigitalOcean Droplet is useful for small projects, but sometimes 512MB of RAM can feel quite limiting. I found myself in a situation where I was using an oversized droplet for just a static webpage and a mail server. My goal was to downsize to the smallest possible droplet. Upon monitoring, I discovered the RAM usage hovered around 500MB—dangerously close to the 512MB limit of the lowest-tier droplet. That’s when the idea of leveraging SWAP memory to augment the RAM came to mind.
Adding a Swap File to Your Droplet
If your applications occasionally run out of RAM, incorporating a swap file can prevent memory exhaustion. It’s important to remember, though, that relying too much on swapping can decelerate your application because swap file operations (read/write to the HDD) are slower than those performed in RAM.
- Log into Your Droplet. Securely access your server.
- Check for an Active Swap File. Use the command:
sudo swapon --show
If there’s no active swap, you’re good to proceed. 3. Create a Swap File for the droplet. I opted for a 2GB swap file with
sudo fallocate -l 2G /swapfile
- Set the Right Permissions.
sudo chmod 600 /swapfile
- Designate the File as Swap Space. Use the next command to mark it:
sudo mkswap /swapfile
- Activate the Swap File. Enable it with:
sudo swapon /swapfile
- Ensure Permanency. To have the swap file enabled at boot, append it to your fstab file with:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Conclusion
Low RAM droplets offer great value, and with this straightforward adjustment, you can enhance their utility even further. This enables running more RAM-intensive applications without the need for a more expensive upgrade.