Increase PHP memory limit in WordPress
Sometimes when you are trying to perform a normal WordPress action like activating a plugin, you get an error message like this:
Fatal error: Allowed memory size of 157286400 bytes exhausted (tried to allocate 5775295 bytes)
If you are running resource-intensive plugins like WooCommerce, or maybe if your host is stingy with resources, you can find yourself hitting that error.
This means that PHP, the language that WordPress runs on, needs to be able to use more memory to complete the task you just attempted.
The solution is to increase the amount of memory allocated to PHP.
There are a few ways to do this, and you may or may not be limited by your host, in which case, if the following fixes don’t work, you need to talk to them. If you max out your memory and continue to have problems, the issue might be a problematic plugin or some poor code in your theme.
Make sure you run an up-to-date version of PHP since older ones are less efficient.
- A
-
Via wp-config.php
define('WP_MEMORY_LIMIT', '256M');
WooCommerce recommends at least 64M. 128M should be enough for most sites, but if you run resource-intensive plugins, you may have to go higher. You can increase it incrementally until you can complete the task that triggered the message.
To increase the memory limit for the administration area:
define( 'WP_MAX_MEMORY_LIMIT', '256M' );
- B
-
Via PHP.ini
If you find that defining the limit in the wp-config file is not working, you might have to address it a level up - that is in your PHP configuration file, known as php.ini.
Depending on your host, you may or may not have access to this file, and you may or may not be able to create your own. Search your webhost’s knowledge base for php.ini and you should come up with where to locate the file.
Then add, or adjust the following line:memory_limit = 128M ; Maximum amount of memory a script may consume (128MB)
- C
-
Via .htaccess
-
If you don’t have access to your php.ini file, sometimes you can use your .htaccess file to set the memory limit:
php_value memory_limit 128M
How much PHP memory is enough?
Go slowly, increasing it by steps, and test again: 128M, 256M, 512M.
Why? Each Apache child process is allowed to use the memory limit you set. If you have 10 PHP scripts running at the same time, each script will be allowed to reach that limit.
That's why setting the memory_limit to a very high value might be an issue too, this limit is also protecting your server by blocking inefficient PHP code. If you allocate too much memory, and there are poorly coded scripts, it might be counterproductive because your server could start eating into disk swap space.
If you are in doubt, please ask your hosing provider about this.