Improve Site Performance by Increasing PHP Memory for WordPress

[ ~:{*}:~ ] During the recent ASO server debacle, I raced frantically to restore functionality to Perishable Press. Along the way, one of the many tricks that I tried while trying to fix the dreaded “white screen of death” syndrome involved increasing the amount of PHP memory available to WordPress. This fix worked for me, but may not prove effective on every installation of WordPress. If you are unsure as to whether or not you need to increase your PHP memory, consult with your host concerning current available memory 1 and overall compatibility with a localized increase. Note that if your blog is running fine, there is probably no need to employ this solution. I recommend this change only if you are experiencing PHP-related and/or PHP memory issues associated with any of the following:

  • Inability to open various WP Admin screens (e.g., Themes, Plugins)
  • Inability to leave comments or no returned page upon leaving a comment
  • Inability to search or send feedback via various comment forms
  • Broken plugins (e.g., XML Sitemap Generator, DD Sitemap Generator)
  • Experiencing various timeouts (e.g., file uploads, page loads)
  • Unexpected downloading of PHP files via Firefox
  • Explicit PHP memory errors

If you are experiencing any one (or more) of these various issues, increasing your blog’s PHP memory limit may be the solution.

Increase PHP Memory for WordPress

First and foremost, backup your files — especially, backup your blog’s wp-config.php file (typically located in the WordPress root directory). Once you have done that, open your wp-config.php file and place the following code directly after the opening PHP element as follows:

<?php 
@ini_set("memory_limit","16M");
.
.
.

Save and upload the file and check your site. If you are still experiencing problems with any of the above-listed issues, try emptying your WordPress cache (if applicable) and/or deleting and re-creating your WordPress object cache. Then, if things are still acting bonkers, you may try incrementally increasing the amount of available PHP memory. Depending on your current settings, you may increase memory to the following values:

@ini_set("memory_limit","12M");
@ini_set("memory_limit","16M");
@ini_set("memory_limit","32M");
@ini_set("memory_limit","64M");

Targeting specific files

By maintaining and investigating a PHP error log, it may be possible to determine exactly the file or script that is causing the memory errors. For example, ever since my hosting company upgraded their servers to Apache 1.3.41, the WordPress cache.php file has been generating a large number of PHP memory errors similar to the following:

[17-Feb-2008 19:26:38] PHP Fatal error:  Allowed memory size of 33554432 bytes exhausted (tried to allocate 6233929 bytes) in /home/.../press/wp-includes/cache.php on line 330
[17-Feb-2008 19:30:57] PHP Fatal error:  Allowed memory size of 33554432 bytes exhausted (tried to allocate 6327989 bytes) in /home/.../press/wp-includes/cache.php on line 330
[17-Feb-2008 19:33:35] PHP Fatal error:  Allowed memory size of 33554432 bytes exhausted (tried to allocate 6253909 bytes) in /home/.../press/wp-includes/cache.php on line 330

Thus, given this information, I decided to target the source of the issue, by increasing memory exclusively for cache.php. Although results may vary, in general, directly targeting the source of the conflict may work better than globally applied specifications. I will be writing more on the memory errors associated with the WordPress cache.php file in the near future.

Wrap up..

Remember that your specific hosting environment will affect the degree to which this technique proves effective. Also note that it is possible to raise your memory via php.ini or even htaccess. Further, the memory-increasing techniques described in this article utilize common PHP directives and may be applied to virtually any PHP-driven script or platform. As always, share your insights with our growing community!

Notes

  • 1 To check your available PHP memory, upload a phpinfo.php file containing <?php phpinfo(); ?> to your server and call it in a browser. Scroll down about one screen and locate the line that says, “memory_limit” — the value given represents your server’s current PHP memory limit.