How to strip down Amazon Linux EC2 to maximize available RAM

Firstly, this article mainly applies to low memory Amazon Linux EC2 instance types such as t2.nano and t2.micro. The instructions below are also specific to Amazon Linux AMI, which like CentOS, is based on RHEL (Red Hat Enterprise Linux). Yum!

Strip down Amazon Linux EC2 to maximize available RAM

Update all packages – yum package manager

First, login to your new Amazon Linux EC2 instance and update all packages:

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

Reduce the number of getty services

Edit /etc/sysconfig/init and replace:




Replace with agetty with mingetty

Update! – AWS now installs mingetty by default. In which case if the output of the install command is “already installed”, then simply make the line change to the /etc/init/serial.conf file as described below.

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


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

Disable yum-updatesd and replace it with a simple cron job.

Update! – AWS no longer installs yum-updatesd by default. You can still setup the cron if you’d like. Check to see if yum-updatesd is installed using this command to list: chkconfig

This will save resident memory.


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

Followed by:

Disable IPv6 support

Disable Network Time Protocol (NTP) daemom

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 Amazon Linux EC2 instances.

Tuning Amazon Linux EC2 swappiness and cache pressure

Another method of squeezing the most from your Amazon Linux EC2’s limited RAM, is to tune the system’s swappiness (tendency to swap) and cache pressure (tendency to reclaim cache).

Read:  Raspberry Pi 3 Overclock and Turbo Config

swappiness (Recommended value 10 to 60. 0 if you don’t have swap added) – This control is used to define how aggressive the kernel will swap memory pages. Higher values will increase agressiveness, lower values decrease the amount of swap. (default = 60)

vfs_cache_pressure (Recommend value 50 to 200) – Controls the tendency of the kernel to reclaim the memory which is used for caching of directory and inode objects. (default = 100)

Add these lines to the end of the /etc/sysctl.conf file.


Increasing the cache pressure may be somewhat counter productive since caching is good for performance. However, swapping too often can also reduce your server’s overall performance. Use this for example if free -m shows say more than 60% of RAM being used by cache/buffers …remember this NOT a bad thing! However, depending on what is being cached you may reduce memory usage/contention and as a result gain performance by adjusting.

To check current values using these commands:

To enable these settings without rebooting use the following commands:

How To Add Swap on Amazon Linux EC2

Of course, on low-memory instances swap is 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.

Note: this article was originally published on Nov 21, 2013. It has been updated to ensure that the suggested changes are still compatible.

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

Tags: , , , , ,

How to strip down Amazon Linux EC2 to maximize available RAM

6 Responses

  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 #
  2. It is actually not recommended to turn of ntpd. Due to the nature of virtualization (being put to sleep sometimes), the clock can drift significantly in a relative short time.

    Kalith April 22, 2016 at 4:38 pm #
    • Thanks, the cron can be set < weekly as well. These are options based on preference for tiny instances.

      Hayden James April 22, 2016 at 5:17 pm #
  3. As noted above, this article has been updated for compatibility with current nano and micro EC2 instances.

    Hayden James May 30, 2016 at 1:14 am #
  4. You need to be careful creating swap partitions as nano and micro instances only use network EBS storage.

    The last thing you want is for your machine to start swapping and having to a network drive.

    Paul July 8, 2016 at 11:52 am #
    • True. Perhaps aadjusting the swappiness and cache pressure would be wise if using swap.

      Hayden James July 15, 2016 at 5:47 pm #

Leave a Reply