Fixing low memory errors in Amazon EC2 (Ubuntu)
20 Aug 2014Amazon’s AWS offers a free tier that provides a year’s worth of a micro instance along with a decent amount of storage and other services. If you use an EBS volume for your instance you can easily upgrade your machine to more powerful instances for cheap. For the most part, the micro instance is plenty of power for basic development I do. However, occasionally I’ll need an extra boost. Most recently I ran into problems running cabal update
during my Haskell learning adventures.
Sometimes a process will terminate for no reason - especially during compilation. If this happens to you, run dmesg
and check for an “Out of Memory” error.
While you could shutdown the instance and upgrade to a larger version (for a price), an easier way is to temporarily add swap space. When a computer doesn’t have enough physical RAM, it can use swap space as virtual memory. While slower than hardware, this space lets you increase the physical memory seen by the machine.
Without further ado, let’s add some swap space:
First check to see if you have any swap space in the first place
sudo swapon -s
If you have no swap (as Ubuntu EC2 doesn’t), you should see an empty list
Filename Type Size Used Priority
Next double check your disk usage. The swap file is hard disk space, and if you don’t have enough this won’t work. Run df -h
to get a human readable output.
We have plenty of space available. We’ll add a 256K swap partition, which will increase our memory by 50%.
If all goes according to plan you should be able to see the swap partition by running swapon -s
This file will disappear on reboot, making it good for when you need to run big jobs infrequently. If you want to make this swap permanent you can follow instructions at this DigitalOcean post.