PHP memory limit set but web host still complains about excessive memory usage



Note: This article contains troubleshooting advice and doesn’t focus specific fixes.

I had a tough time with a hosting company that kept complaining about excessive memory usage. Allowed memory usage was 128MB but in some cases the memory accesses was jumping to 150MB and even 175MB. Insome cases. Let me explain what applications I was using on the web hosting. The website is built with WordPress 3.0 and has GetShopped e-commerce plugin3.7.1 installed. The site has only one product with variations and other customization options. The website gets around 200 unique hits a day.

When I was testing the website on the live server, the hosting company didn’t complain about any memory issues. Once the site was officially launched, that is when we started getting alerts from the hosting. These alerts came from the index.php and another file that handles cron jobs in WordPress. The hosting company told us that something was wrong with the number of SQL queries i.e. they were written inefficiently and there could even be infinite loops in the web app.

I looked up everywhere on the forums but couldn’t find any one complaining about similar issues. There are some complaints about the the way GetShopped SQL is designed but that was improved in versions 3.7 and over. Updating or rewriting the queries was not an option because the website was already live and taking orders by the hour. So I tried to put a manual memory limit in WordPress.

Editing default-constants.php in wp-includes

Edit Line#21 default-constants.php in the wp-includes folder.

define(‘WP_MEMORY_LIMIT’, ’64M’);

I changed it to 64MB. The plugin works fine with 32MB but to be on the safe side, I kept it at 64MB. However, my hosting company still complained about memory issues.

Editing .htaccess to limit PHP memory

To set php memory limit through .htaccess, I put the following code in the .htaccess file:

php_value memory_limit 64M

But this gave me a 500 Internal Server Error on the website. I talked to the web host and they told me this method was not supported by the server. So make sure your host supports this method before you try it on live site.

Enable HTTP/PHP/ SQL caching

I checked the number of SQL queries the website was generating. Before any caching, the max queries were around 90 which isnt alarming. After caching, 99% of these queries were cached. I used a plug-in called Super Cache for WordPress for this task. Alas, the web host still complained.

I started getting memory warnings for other websites on the same hosting

I had two more websites hosted with the same hosting company. Both were ecommerce websites. One was based on Joomla Virtuemart and other was Magento based store. This is when I began to think something could be up with the web host itself that is causing the alerts. One fact that supports the cause is the website that never generated any warnings for an year but suddenly started causing memory issues. I was left with one last bullet in my gun, the php_ini method of limiting PHP memory.

Using php_ini to limit memory

Put the following PHP code at the top the website i.e. before all PHP scripts:

ini_set(‘memory_limit’, ’64M’)

I have put this at the top of my WordPress theme file. Similar can be done incase of Joomla and Magento. Once I applied this method, the web host never complained again. Yayyy ! Why the other methods didn’t work, I am not sure. If any one knows the reason for this, feel free to educate me .

About Shahab Qamar

Shahab Qamar is a web developer/designer from Brisbane, Australia who loves to share solutions and maintains simplefix.net.