When you saw an error reads “Fatal Error: Allowed memory size of xxxxxx bytes exhausted” either in browser or server logs , that means PHP had exhausted the max memory limit. Mostly, this’s due to the insufficient memory allocated for the script. This article will teach how to increase the momory limit.
Check The Memory Limit
To see how much memory allocated for you php, you need create a file on your server (like view-php-info.php), and put following code in it:
<?php phpinfo(); ?>
Visit it in your browser, you’ll see a table list all the config about current php environment. Search for “memory_limit”, you’ll see:
Also, you can also use this way to variety the memory limit change.
How much memory do I need?
It depends, for wordpress core, 32MB is the default memory limit. For drupal6 core, the min memory limit is 16MB, 32MB is recommended. But if you install many plugins, especially the plugins deal with image, you probably need 128MB or more memory.
Increate The Memory Limit
The most easy and common way is chage the php.ini.(For shared hosting use, please skip this method)
- Firstly you need find your php.ini .
If you did what the last section let you ( create a php file to view php config ), then you can just visit that page, and search for “Loaded Configuration File”, see below:
Otherwise, for linux user, you can simple use “php -i | grep Loaded Configuration File” to find it.
For windows user, you can try to find it in your php install directory.
- Edit php.ini .
Search “memory_limit” in your php.ini, and change it. If no “memory_limit” found, add the following line at the end of php.ini
memory_limit = 128M ; Change the 128M to your needs
- Restart Apache
Use follow command to restart apache：
For some restricted environment, such as shared hosting user, it’s impossible to change php.ini, you’ll consider the follow ways.
Notice: This method will only work if PHP is running as an Apache module.
Find the “.htaccess” in your root directory of the specified domain, if there isn’t, create one. Put the following line in it.
php_value memory_limit 128M ; Change the 128M to your needs
Way3: Change Memory At Runtime
For WordPress Users
In your wp-config.php, find or add the following line:
WordPress will automatically check if PHP has been allocated less memory than the entered value before utilizing this function. For example, if PHP has been allocated 64MB, there is no need to set this value to 64M as WordPress will automatically use all 64MB if need be.
Notice: By default, WordPress will attempt to increase memory allocated to PHP to 32MB (code is at beginning of wp-settings.php), so the setting in wp-config.php should reflect something higher than 32MB.
For Drupal Users
For drupal, there is another choice, you can edit sites/default/settings.php. Locate the PHP settings section and add the following line at the end of that section:
This method will affect only the site using this file.
For other frameworks
Similarly to Drupal, in your code, you can add the following line in your php code. That will take effect at runtime.
There are many reasons that can lead to increase failed. If you are a shared hosting user, the most possible reason is: Your hosting vendor does not allow for increasing the PHP memory limit, in this case, contact your host to increase the PHP memory limit or change your php hosting.