/ mozey.co / blog

apache2 oom

August 1, 2015

Avoiding the OOM Killer by limiting the number of Apache2 processes and clients

“Out of memory: kill process…”

🔗 Apache memory usage has to be adjusted for EC2 micro instances

View processes sorted by memory usage

echo && echo -e 'PID \t%MEM \tSTART \tTIME \tCOMMAND' && \
ps aux | awk '{print $2, "\t"$4, "\t"$9, "\t"$10, "\t"$11}' | sort -k2rn | head -n 15

Determine if apache2 is running as prefork or worker

apache2 -l

Adjust the config file

sudo vi /etc/apache2/apache2.conf

<IfModule mpm_prefork_module>
    StartServers 2
    MinSpareServers 2
    MaxSpareServers 5
    MaxClients 40
    MaxRequestsPerChild 0
</IfModule>

StartServers

The number of server processes (as seen when we run ps aux or top) 
that apache starts automatically.

MinSpareServers

The minimum number of server processes apache will keep running in reserve, 
so they can be used as needed.

MaxSpareServers

The maximum number of server processes apache will keep running in reserve. 
Any processes in excess of this will be killed.

MaxClients

The maximum number of requests (GET, POST, etc.) that can be fielded at the same time. 
MaxKeepAliveRequests, earlier in this file, determines how many requests each connection 
can make before it has to re-establish. While keeping these numbers high is good for performance 
for your end-user, it’s bad if you have a baby-sized server like me.

MaxRequestsPerChild

Number of requests a child process will handle before terminating.