30 December 2011

Amazon EC2 micro instances are really cheap but come with only a small amount of memory and no swap device configured. If you need more memory and don’t want to pay for an instance with more memory you can define and activate a swap device. Here is how to do it.

First, let’s have a look at the current memory characteristics.

$ free
             total       used       free     shared    buffers     cached
Mem:        611212     563716      47496          0     122920     166032
-/+ buffers/cache:     274764     336448
Swap:            0          0          0

As you can see there is no swap device configured. By the way look at the dmesg output to see how much physical memory is allocated to the instance.

$ dmesg | grep [Mm]em
[    0.000000] released 0 pages of unused memory
[    0.000000] initial memory mapped : 0 - 20000000
[    0.000000] init_memory_mapping: 0000000000000000-0000000026700000
[    0.000000] Initmem setup node 0 0000000000000000-0000000026700000
[    0.000000]   DMA zone: 56 pages used for memmap
[    0.000000]   DMA32 zone: 2097 pages used for memmap
[    0.000000] Memory: 607920k/629760k available (3232k kernel code, 388k absent, 21452k reserved, 3687k data, 500k init)
[    0.229708] Freeing initrd memory: 1164k freed
[    0.356226] Freeing unused kernel memory: 500k freed
[    0.359357] Freeing unused kernel memory: 844k freed
[    0.359980] Freeing unused kernel memory: 768k freed
$ echo 629760/1024 | bc -l

The instance got 615MB of RAM whereas the instance description on the Amazon AWS site indicate 613MB. Looks like I got 2MB for free!
Before adding a swap space, a swap device must be created. It can be a raw partition on disk or a file hosted on a filesystem. No need to pay for an EBS drive as I have available space on the system disks provided with the instance so I’ll choose the second option. The disk space of the swap file need to be preallocated before configuring it as a swap device. I’ll use dd to create a 1GB file.

$ sudo dd if=/dev/zero of=/var/swapfile1 bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 27.2743 s, 39.4 MB/s

Next step is to configure the swap file as a swap device.

$ sudo mkswap /var/swapfile1
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=6ef0ffbf-6644-4a6a-beec-ae11195d4bd5

Finally, activate the swap device with swapon.

$ sudo swapon /var/swapfile1

Let’s verify !

             total       used       free     shared    buffers     cached
Mem:        611212     602640       8572          0      44836     299264
-/+ buffers/cache:     258540     352672
Swap:      1048572          0    1048572

Done. But this change will not be persistant across reboot. To activate this new swap device at boot time, it must be defined in the /etc/fstab file.

$ sudo echo "/var/swapfile1 none swap sw 0 0" >> /etc/fstab

It is always a good thing to verify that changes made to the system are working as expected. Take time to reboot your instance to make sure the swap device is on at boot time.

$ sudo reboot

blog comments powered by Disqus