WordPress: Increase PHP Memory Limit
By default, WordPress consumes very little memory and should run fine on any type of hosting with ease.
At some stage or another, you may need to increase the memory made available to WordPress due to large applications or plugins running big processes.
Increasing WordPress memory limit can speed up plugins
Watch the Video
What determines the available memory?
The available memory which WordPress can use is determined on several levels really.
Physical Memory, OS & Webserver
The core factor that determines memory is literally the physical memory/RAM installed on the computer/server.
Then the operating system and webserver will allocate certain memory usage based on it’s configuration.
Server Side Language/Module
Ultimately, your server side language will determine, set and make available that memory that may be used.
In the case of WordPress, the server side language is PHP so the
memory_limit of PHP will be configured globally on the server, for the domain specifically, WordPress could be setting a memory limit or a specific script could be applying a memory limit.
It is quick and easy to increase memory limit
With PHP error_reporting turned on or WP_DEBUG set to true, your website running out of memory will no longer display a blank page or a 500 Internal Server Error but will then rather show a PHP memory limit fatal error such as this:
Fatal error: Allowed memory size of X bytes exhausted (tried to allocate Y bytes) in /home/xxx/public_html/wp-includes/plugin.php on line xxx
How to Increase PHP Memory Limit
There are several ways that PHP memory limit can be increased in WordPress.
Some may work and others may not so try these different ways:
1. Setting Memory Limit in wp-config.php
The quickest and easiest way is to open your WordPress installation’s
wp-config.php file and set the
WP_MEMORY_LIMIT constant with a value. The constant is not set by WordPress by default so you’ll need to add it at the top of your
wp-config.php file like this:
Change the value
256M to the megabytes of memory you want to allocate.
2. Memory Limit PHP Directive in .htaccess file
Check if there is a
.htaccess file in the root of your WordPress installation. It may be a hidden file. If it is not there, you can create it. Then in your
.htaccess file, you can put the PHP directive at the top:
php_value memory_limit 256M
If the above change gives a 500 Internal Server Error on your website you know that the configuration of PHP doesn’t allow directives to be changed this way. Remove the line and re-save the
.htaccess to remove the 500 error and restore the website.
3. Changing Memory Limit in php.ini
You can either change the main
php.ini file on your webserver’s PHP installation or you can try creating a new
php.ini file inside the root of your website to see if it is effective. Then set the
memory_limit in the
php.ini file like this:
memory_limit = 256M
This shouldn’t cause any problems or errors but it may not be effective depending on your hosting.
4. Set PHP Memory Limit During Runtime
You can try setting the PHP
memory_limit directive during runtime in
wp-config.php or in the script that you’re trying to run. Do that using
ini_set() like this:
5. Ask Your Hosting Provider
If none of the above methods allow you to increase your WordPress memory limit in PHP, you’ll need to ask the hosting provider.
Many hosting providers do not allow these values to be overridden manually by its hosting users for valid reasons while others do provide that flexibility.
Simply contact them via email/phone and ask them to increase your PHP memory_limit for you. If they cannot, it may be necessary to upgrade to a higher hosting plan or even go as far as switching your hosting to a much more powerful WordPress hosting which does allow it.