On one Sunday afternoon, you might decide to spend a lot of time browsing your Joomla website, just to make sure that all the pages are working fine. 15 minutes later, you see the following error:
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 5298178 bytes)
What happened for you to see this error, what is the cause of this error, how can you solve it immediately, and how can ensure that it won’t happen again?
What happened?
The allowed memory size is not a Joomla error, it’s a PHP error (here’s a list of all the possible errors that may occur on a Joomla website). It happens when PHP needs more memory to run your scripts, but is unable to allocate more memory because its available memory limited to 128 MB (128 MB = 134217728 bytes – note that 128 MB is just an example, your memory limit might be smaller or higher). By default, the memory limitation is determined in the php.ini file.
What is the cause of this problem?
Joomla is a memory hungry CMS, and sometimes it takes a lot of memory to process a page, especially if that page contains many 3rd party extensions that are also heavy on the memory. On a recent job, the customer was using an extension that was creating an array out of a 200,000 row MySQL table. The extension author was not aware that his extension will be used to process such a large amount of data, and thus has not optimized it. Naturally, our customer was running into the “Allowed memory size…” error every time the extension ran.
How can this problem be solved immediately?
The fastest and most expedient way to solve this problem is by increasing the PHP memory limit. The PHP memory limit can be increased in one of the following 4 ways:
- Increasing the PHP memory limit at the server level (global php.ini): This can only be done if your website is running on a dedicated server or on a VPS. What you do is that you edit the php.ini file and change the memory_limit value to a larger amount. Once you do that, you will need to restart Apache in order for the changes to take effect.
-
Increasing the PHP memory limit at the application level (local.php.ini): This can be done by just making a copy of your global php.ini, changing the memory_limit value to a larger amount, and then uploading the modified copy to the root directory of your Joomla website (e.g. /public_html/ or /public_html/yourjoomlawebsite.com/). (You will need the server administrator to send you the global copy of php.ini in order to do this.)
-
Increasing the PHP memory limit at the .htaccess level: This can be done by adding the below line to the beginning of your .htaccess file (which is located in the root directory of your website):
php_value memory_limit 512M
The above code will increase the PHP memory limit to 512 megabytes.
-
Increasing the PHP memory limit at the code level: This can be done by adding the following line to your beginning of your index.php (which is located in the root directory of your Joomla website – do not add it to your configuration.php, because any change in your configuration settings will overwrite your configuration.php and your custom changes will be lost):
ini_set('memory_limit', '512M');
Now what is the best way to increase the memory limit? We believe the best way to increase your memory limit is either by doing it in the index.php file or in the .htaccess file. This will ensure that if you decide to switch hosting companies, then your PHP memory settings will remain the same.
How to ensure that you won’t see the problem again?
Increasing the memory limit will not address the underlying issue causing this problem, which is bad code. The best way to solve this problem once and for all is to optimize the code causing your Joomla website to exhaust all the memory allocated for the PHP instance. In some cases, increasing the memory limit alone will not solve the problem at all (you will still see the fatal error). In this case you will need to fix the code.
Are you seeing the “Allowed Memory Size…” error on your website without being able to get rid of it? If this is the case then fear not, we’re here to help! Just contact us and we’ll solve it for you in no time – and rest assured: our work will not cost you much!
As always excellent advice from itoctopus. I opted on adding the the code to my index.php file. Thanks for the information.