How to unload Amazon Linux EC2 to maximize available RAM?

First of all, this article mainly applies to Amazon Linux EC2 with low memory instance type such as t2.nano and t2.micro. The instructions below are also specific Amazon Linux AMIwho likes Centosbased on RHEL (Red Hat Enterprise Linux), Note: This article is from 2016 and only applies to Amazon Linux version 1.

update all packages – yum package manager

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

sudo yum update

Then if you are not going to use MTA (Mail Transfer Agent) you can Disable dispatch service,

sudo chkconfig sendmail off
sudo service sendmail stop

Reduce the number of Getty services

edit /etc/sysconfig/init and change:




Replace Azty with Minghetti

update! – AWS now installs Minghetti by default. In that case if the output of the install command is “already installed”, just change the line in /etc/init/serial.conf file as described below.

since agent heavy on RAM, come on replace with mingetti,

Install Mingetty first:

sudo yum install mingetty

Then edit /etc/init/serial.conf and change:

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


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

Disable yum-updates and replace it with a simple cron job

update! – AWS no longer installs yum-updates by default. You can still set up cron if you want. Check to see if yum-updatesd is installed using this command in the list: chkconfig

This will save resident memory.

sudo chkconfig yum-updatesd off


sudo yum remove yum-updatesd

Next, make yum update cron instead. Add a new file /etc/cron.daily/yum.cron with the content:

/usr/bin/yum -R 120 -e 0 -d 0 -y update yum 
/usr/bin/yum -R 10 -e 0 -d 0 -y update


sudo chmod +x /etc/cron.daily/yum.cron

Disable IPv6 Support

sudo chkconfig ip6tables off

Disable the Network Time Protocol (NTP) DEMO

next, disable ntpd, Run “top” and press shift+M, depending on memory usage, you’ll see ntpd is near the top of the list. This is used to keep your server’s clock in sync. You can replace with weekly cron so that you can disable the service and further reduce the memory usage.

sudo service ntpd stop
sudo chkconfig ntpd off
sudo chkconfog ntpdate off

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

#! /bin/sh


sudo chmod +x /etc/cron.weekly/ntpdate-sync

To run test:

sudo /etc/cron.weekly/ntpdate-sync

The output should be something like this:

11 Oct 22:57:49 ntpdate[1174]: adjust time server offset -0.017816 sec

These are some basic steps to reduce the memory consumption of the first boot. Smaller Amazon Linux EC2 instances deserve more attention.

Swapping Amazon Linux EC2 and Tuning Cache Pressure

Another way to squeeze the most out of your Amazon Linux EC2’s limited RAM is by tuning the system’s propensity to swap and cache pressure (tendency to reclaim cache).

swappiness (Recommended value 10 to 60.0 if you have not added swap) – This control is used to define how aggressively the kernel will swap memory pages. Higher values ​​will increase aggression, lower values ​​will reduce swap volume. (default = 60)

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

add these lines at the end /etc/sysctl.conf file.


Increasing caching pressure can be counterproductive to some extent because caching is good for performance. However, frequent swapping can also reduce the overall performance of your server. For example use this if free -m show says more than 60% of RAM is being used by cache/buffers… remember this is not a bad thing! However, depending on what is being cached you May Minimize memory usage/contention and gain performance by making adjustments as a result.

To check current values ​​use these commands:

sudo cat /proc/sys/vm/swappiness
sudo cat /proc/sys/vm/vfs_cache_pressure

Use the following command to enable these settings without rebooting:

sudo sysctl -w vm.swappiness=10
sudo sysctl -w vm.vfs_cache_pressure=200

How to Add Swap on Amazon Linux EC2?

Of course, on low-memory instances change is intelligent. For example to add a 1GB swap file, from the command line you would type:

sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576

Now setup the swap file with the command:

sudo mkswap /swapfile
sudo chmod 600 /swapfile

Now enable swap:

sudo swapon /swapfile

If you use the top command, you should now see 1GB of swap added. So now let’s make swap persistent so that it doesn’t get 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 free either df -h command to check for swap.

Remember, adding swap can help keep your server from running out of memory, but not if it’s already using (aka) a large chunk of swap. exchange), it is never good for performance. A lot can be extended regarding swap and paging/swapping. However, today the point is to take apart/tuning the AMI.

Note: This article was originally published on November 21, 2013. Updated to make sure suggested changes are still compatible.

See also: Strip down Apache to improve performance and memory efficiency

Leave a Comment