Add Swap File to Amazon EC2 Instance

I’m playing around with a free-for-a-year micro instance of Amazon’s Elastic Compute Cloud (EC2), and I noticed that while there’s around 600 MB of memory, there was no swap set up! That can grind things to a halt pretty fast. So I set one up:


[root@tauceti ~]# free
total used free shared buffers cached
Mem: 605060 596996 8064 0 68568 440104
-/+ buffers/cache: 88324 516736
Swap: 0 0 0

[root@tauceti ~]# df -k
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/xvde1 6190664 1344280 4531916 23% /
tmpfs 302528 0 302528 0% /dev/shm

[root@tauceti ~]# dd if=/dev/zero of=/swapfile1 bs=1024 count=524288
524288+0 records in
524288+0 records out
536870912 bytes (537 MB) copied, 14.8886 s, 36.1 MB/s

[root@tauceti ~]# df -k
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/xvde1 6190664 1870116 4006080 32% /
tmpfs 302528 0 302528 0% /dev/shm

[root@tauceti ~]# mkswap /swapfile1
Setting up swapspace version 1, size = 524284 KiB
no label, UUID=767b5917-4ff4-453e-bb3a-db644a7a7824

[root@tauceti ~]# chown root:root /swapfile1
[root@tauceti ~]# chmod 0600 /swapfile1
[root@tauceti ~]# swapon /swapfile1
[root@tauceti ~]# echo '/swapfile1 swap swap defaults 0 0' >> /etc/fstab

[root@tauceti ~]# free
total used free shared buffers cached
Mem: 605060 597368 7692 0 68576 440104
-/+ buffers/cache: 88688 516372
Swap: 524280 0 524280

Share and Enjoy:
  • Print
  • Facebook
  • Twitter
  • PDF

Leave a Reply

Your email address will not be published. Required fields are marked *