Adding virtual memory to AWS EC2 micro instances (and use a 32-bit os)

Some of you have probably experienced that an Amazon Web Services EC2 micro instance might be too small with it’s 613MB of memory. If you deploy medium or large footprint application on it (well, even WordPress + MySQL could be stretch), both the instance and your systems might start to crash.

Tip #1: Use a 32-bit image

Using a 64-bit OS sets you at a disadvantage since everything 64-bit actually uses more memory. A memory reference in a 64-bit OS is twice the size of a 32-bit OS, so for micro-instances, be sure to go 32-bit.

A lot of people want to use 64-bit to have an upgrade path. But frankly; both the EC2 small and EC2 medium has under 4GB memory, so stay 32-bit if you are planning to use a micro-instance. The 32-bit alternative gives you a decent upgrade-path, and if your systems grow larger than medium size, a reinstall is probably required anyway.

Tip 2: Add virtual memory

The EC2 micro has, as mentioned above, just a puny 613MB memory allocation, and there is no virtual memory allocated by default. This is however easy to fix.

Step 1:

First, create a EBS-volume with for example 1 GB (if you want to add 1 GB virtual memory).

Step 2:

After the EBS-volume is creates, attach it to the EC 2 micro instance (remember which device it is attached to (for example /dev/sdf)

Step 3:

Logged into your micro-instance, attach the new drive by doing (assuming the volume was mounted as /dev/sdf and you run Ubuntu 12.04 or later):

mkswap -f /dev/xvdf
swapon /dev/xvdf

The virtual memory should now be attached. You may verify it by issuing the command:

free -m
Step 4:

Attach the swap-area permanently so it survives reboots.
Edit your /etc/fstab file and add the line:

/dev/xvdf       swap    swap    defaults        0       0

15 thoughts on “Adding virtual memory to AWS EC2 micro instances (and use a 32-bit os)

  1. I am not sure you know the difference between 64-bit and 32-bit architecture. A 64-bit system will have more memory addresses for a running program provided it can make use of it, and not just a confusing “everything 64-bit actually uses more memory.” – you may need to reword that if you know what you’re talking about..
    I have been working with 64-bit and 32-bit servers for almost 10 years now, your first paragraph sounds a little ignorant than informative.
    From my experience, i have never had an issue opting 64-bit in terms of upgrade path or usability.
    And lastly, go with a local swapfile instead a remote volume for swap storage if you’re on the cloud, the network latency even though it is in the cloud far outweighs using a separate partition for swap.

    1. Hi, I’m quite familiar with the difference between 32 and 64 bit architecture. The issue is that the micro-instance of Amazon EC2 only have 613MB available memory. If you install a 64-bit version of an operating system, it will consume more memory that a 32-bit version since all pointers, internal data structures etc. are 64-bit and not 32-bit. An address-pointer in a 64-bit OS consumes 8 bytes of data, but in a 32-bit OS, it consumes 4 bytes of data.

      The article has nothing to do with upgrade-path for 32 vs 64 bit systems, but are adressing a very common problem on Amazon EC2 instances – mainly that the available memory is quite low. By going with a 32-bit version of an OS instead of 64-bit, you actually are consuming less memory in the running set. Maybe you should try yourself and install a 32-bit version and a 64-bit version in parallell and compare the memory footprint?

      Regarding a SWAP-partition/file; There is no such thing as a “local” disk on Amazon EC2

  2. This was really helpful for a failed install (because of memory limits). Otherwise I would have permanently had to switch to a higher tier just to do the one installation. Thanks!

  3. Hi,
    complete newbie to AWS/Ubuntu/terminals etc here

    I think I have got to stage 2 ok. I can see the dev/xvdf on the file system

    However, when I try mkswap -f /dev/xvdf I get permission denied
    I did this from my home directory /home/ubuntu

    I tried cd/ to get to root and running same command with no difference in outcome

    I have no password. I rely on keypairs to connect

  4. excuse me but are there instructions on how to add more RAM that for a Micro Windows 2008 SQL Server 2008 R2 Installation

    1. It is probably possible to add virtual memory to Windows as well. Just add the disk and put the swap-file on the new disk. I haven´t tried it myself thou…

  5. Very helpful post: I needed virtual memory in order to install certain packages for an EC@ micro-instance of the R Studio Server, and found precisely what do do here!

Leave a Reply