Apache is popular, but it often gets a bad rap for poor performance.
Apache high memory usage is found to cause slow sites, page timeouts, and more.
And here at 1 onlyhost, our Support Engineers help web hosts, digital marketers and other online businesses keep their Apache servers super fast and stable.
Today, we’ll go through the top 5 reasons we’ve seen for Apache high memory usage and how we fix them.
How to reduce Apache’s high memory usage?
Apache comes with a lot of default settings.
And, these default settings can unintentionally become one of the major causes for the high memory usage.
Firstly, let’s take a look at the Multi Processing Module (MPM) of Apache.
For e.g., By default, most Apache softwares come with Prefork module. But, for high traffic servers Worker MPM is the better option. So, by tweaking the settings as per the actual requirement of the customer, we can reduce the memory usage and increase the performance.
Note – Apache settings are subjective to web-traffic volume and system resources. So, prior to making changes, there needs to be a careful analysis.
It is at this point that things can get really technical.
Let’s take a look at how we reduce Apache high memory usage at 1 onlyhost.
[Do you know that Apache optimization helps you to get best out of your server? We can do Apache performance tuning in your server.]
1. Minimize Simultaneous Connections
When simultaneous connections increases, the amount of RAM Apache uses also increases.
Some of the parameters that we tweak to reduce the number of simultaneous connections are :
- MaxClients
- MaxSpareServers
- MinSpareServers
- StartServers
- MaxRequestsPerChild
- KeepAlive
- KeepAliveTimeout
- TimeOut
For e.g., MaxClients
sets the limit on the number of simultaneous requests that can be supported by Apache webserver. So, if it is too high, it can overload the memory. We calculate an ideal value for MaxClient based on the available RAM and process size.
Similarly, KeepAlive
is used to keep a single connection alive to transfer all the files to load a page. This saves the time in establishing a new connection for each file and helps to speed up Apache. But while the connections are kept open, they occupy RAM that could be used for other services.
So, to ensure there is no resource wastage, we set the value of KeepAliveTimeout
to 2-5 seconds in low-traffic and 10 seconds in high-traffic webservers.
And, when dealing with live Apache servers, any tweak will have a wide impact. That’s why, our Dedicated Engineers always do rigorous testing using Apache load testing tools like ‘ab‘.
Click here to have our experts preform these tweaks while constantly monitoring for any configuration issues that may pop up.
2. Replace Prefork with Worker MPM
Prefork is the default module for Apache. It uses multiple child processes. Each child handles one connection at a time.
But the memory usage with Prefork is high.
So, if you have a high traffic, then the corresponding memory usage will be excessive.
In such cases, Worker MPM is the better option. It uses multiple child processes with many threads each. Each thread handles one connection at a time. So, the memory footprint is comparatively lower.
3. Reduce the number of modules
Many users may not even bother about optimization initially when setting up Apache. They go ahead with adding many modules.
As a result, Apache server ends up with a long list of modules, while you may not actually need all of them.
And Apache loads all these modules to server memory. This can further slow down your server.
For e.g., Consider the following modules which are loaded by default on CentOS 6.x.
- proxy_balancer_module (shared)
- proxy_ftp_module (shared)
- proxy_http_module (shared)
- proxy_ajp_module (shared)
- proxy_connect_module (shared)
- cache_module (shared)
If any of these are not needed, we can disable them to reduce the memory usage.
NOTE – Before you disable modules, ensure to keep a backup. In this way you can revert the changes if needed. Also, along with disabling modules, make the corresponding changes in the config file. Otherwise it may show errors. If you would rather have us do these optimizations for you, then click here to talk to our experts.
4. Reduce Log level
Apache generates a lot of log info depending on how busy the site is.
If you have alternate ways to gather visitor data (eg. Google Analytics), the memory usage can be reduced by logging only error messages instead of all access logs.
Another alternative is to use Piped Logging, where Apache passes the log data to an external process (like logrotate). This can also minimize Apache’s memory footprint.
One of the best method that we follow here is to raise the log level only when troubleshooting a critical issue. And, after collecting the logs, our Support Engineers revert back the configuration.
5. Optimize PHP and MySQL
Web servers rarely function alone. It works in sync with the database servers too. And often, an issue with the underlying PHP or MySQL is taken as memory issue with Apache.
So, if even after making the configuration tweaks in Apache doesn’t resolve the high memory usage, we look for the probable cause in other places.
For e.g., By default Apache uses mod_PHP
. But, by using PHP-FPM
we can limit the memory consumption and also improve performance.
Similarly, in MySQL we can adjust the Key buffer size, Query Cache, Table Cache parameters etc., to reduce the memory usage.
Conclusion
Apache high memory usage can have an adverse effect on performance of websites. In this post, we have discussed the various techniques our experts at 1 onlyhost use to reduce the high memory usage.