How to strip down Amazon Linux EC2 to maximize available RAM

Firstly, this article mainly applies to low memory Amazon EC2 instance types such as t1.micro, m1.small and c1.medium. For example, the t1.micro Amazon EC2 instance will often have only around 500 megabytes “free” RAM on boot. The instructions below are also specific to Amazon Linux AMI, which, like CentOS, is based on RHEL (Red Hat Enterprise Linux). Yum!

Lets get started, login to your new EC2 instance as root and update all packages:

Then if you are not going to use MTA (mail transfer agent) you can disable sendmail service:

Next, reduce number of getty services. Edit /etc/sysconfig/init and replace:

ACTIVE_CONSOLES=/dev/tty[1-6]

with…

ACTIVE_CONSOLES=/dev/tty[1-1]

Since agetty is heavier on RAM, lets replace with mingetty.

Install mingetty first:

then edit /etc/init/serial.conf and replace:

exec /sbin/agetty /dev/$DEV $SPEED vt100-nav

with

exec /sbin/mingetty /dev/$DEV $SPEED vt100-nav

Disable yum-updatesd and replace it with a simple cron job. Will save resident memory.

or

Next, create yum update cron instead. Add a new file /etc/cron.daily/yum.cron with contents:

Followed by:

Disable IPv6:

Next, disable ntpd. Run “top” and press shift + M to sort by memory usage, you’ll notice that ntpd is close to the top of the list. Its used to keep your server clock in sync. You can replace with weekly cron so you can disable the service and further reduce memory usage.

Now add a new file named ntpdate-sync to the /etc/cron.weekly directory with the contents:

Followed by:

To test run:

Output should be something like:

These are some basic steps to lower memory consumption of first boot. More noticeable on the smaller EC2 instances.

Of course, on low-memory instances swap is especially wise. To add a 1GB swap file for example, from command line you’ll type:

Now setup the swap file with the command:

Now enable the swap:

If you use the top command, you should now see the 1gb swap added. So now lets make swap persistent so it’s not dropped when you reboot. Edit /etc/fstab file and add this line as the last line:

/swapfile swap swap defaults 0 0

When you reboot, use the free -h or df -h command to check for swap.

Remember, adding swap can help save your server from running out of memory but if it’s already using a big chunk of swap (aka swapping), that is never good for performance. A lot can be expanded upon with regards to swap and paging/swapping. However, the point today is that stripping/tuning the AMI, as well as the services you’ve installed should always be your focus and then of course upgrade to a larger instance if all else fails.

Also see: Strip Down Apache to Improve Performance & Memory Efficiency

How to strip down Amazon Linux EC2 to maximize available RAM was last modified: November 21st, 2013 by Hayden James
How to strip down Amazon Linux EC2 to maximize available RAM

One Response

  1. I’m following these commands on Amazon Linux AMI 32-bit and it already had mingetty installed also there was yum-updatesd.

    Eduardo Mello March 29, 2014 at 5:34 am #

Leave a Reply