How to: Increase the memory being allocated to PHP
As part of a series of posts regarding how to improve performance when using WordPress, this particular one will focus on the key aspect of memory available to PHP.
As most readers would agree, available memory is an important aspect of any application and its performance. WordPress is no different. Out of the box most installations have the default 64mb available for PHP and WordPress. Not all hosts are configured the same but if you have your own generally this are the default settings. Depending on the load you have, number of PHP processes, available memory, etc. tweaking those settings will have an impact on your performance.
Most of the tweaking has to be done on a case to case basis (generally you want to be able to provide as much memory as possible but within reason). But regardless, the key is to know where you need to modify those setting so you can enjoy the benefits of having more memory available to your WordPress installation:
I. Increase memory_limit in PHP.ini
The first thing you want to consider is increasing your memory limit in PHP. The memory_limit directive specifies the maximum amount of memory a script may consume (by default 64mb). If you have an old installation or a shared host you might find yourself with a lower value so taking a look at this setting should be your first step. Remember that the Memory Limit is assigned per process. So if you have PHP FPM and 10 processes, you are going to potentially need up to 640mb in available RAM. Another thing to keep in mind is that this value impacts all your PHP applications (or you could segregate the setting per pool on PHP FPM, but if you share pools then you share the setting).
If you are using a shared host you might not be able to modify the php.ini file. In that case adding the following line to your .htaccess file might do the trick:
php_value memory_limit 64M
II. Increase WP_MEMORY_LIMIT in wp-config.php
The next step is inscreasing the WP_MEMORY_LIMIT value in your wp-config.php file. As you can probably guess by now, the memory_limit in PHP.ini is useless if you don’t increase WP_Memory_Limit in wp-config.php. Because of shared pools and the like, WordPress starting with version 2.5 uses the WP_MEMORY_LIMIT option to precisely limit the maximum amount of memory that can be consumed by PHP. What WordPress would do is attempt to increase the memory allocated to PHP only when running WordPress. So theoretically if your host/configuration allows it, your memory_limit could be set to 32M while your WP_Memory_Limit to 64M and WordPress will try to change on the fly the memory allocation for its processes to 64 Megabytes.
III. Increase WP_MAX_MEMORY_LIMIT in wp-config.php
This one is my favorite setting and also one that I haven’t seen much written about. WordPress realizes as most of us have with time, that the administration area is more resource intensive than the general website. And it makes sense, after all, that polling all post vs displaying only one is more demanding. What many of us ended up doing was allocating 128 or even 256 Megabytes to PHP’s memory_limit and wp-config.php’s WP_MEMORY_LIMIT which resulted in increased performance of the administration area but then again, excessive consumption for the other processes handling regular visitor/user requests. To address this issue WordPress WP_MAX_MEMORY_LIMIT option which establishes the maximum memory that can be used on the administration area. Ironically, you could also provide a lower value than WP_MEMORY_LIMIT effectively decreasing the memory assigned when in the administration area. Pretty cool eh?
Be sure to provide your WordPress installation with sufficient resources to handle the incoming traffic from your visitors. WordPress recommends at least 40 MB allocated to PHP. Obviously the more the better but within reason. Be sure to configure the three settings that govern the memory allocation so that one of the settings doesn’t restrict the others effectively nulling what you’re trying to accomplish.