A week ago I did an upgrade of the site to the latest version of WordPress, but I kept getting an error Fatal Error: PHP Allowed Memory Size Exhausted then it would give the name of a script and a line number. Browsing around the net, I was able to figure out the problem and find some solutions.
The problem is an issue with the default setup of PHP, where is has a memory limit of 8 MB. This isn’t necessarily a big issue, but some more sophisticated scripts require more memory of the server, and since PHP is limiting how much memory is allocated, it returns a fatal error if a script wants too much.
The first solution is to change the memory_limit section of the php.ini file on the server. However, since this site is hosted, I don’t have access to that for security reasons. Also, most hosts are not inclined to change those setting for fear they will break other sites they host. Makes sense.
The second solution is to add the line php_value memory_limit 32M to the .htaccess file in the root directory of my site. I did this and it didn’t work. I tried it with different memory values with no success. I tried using M, MB, and Mb, also with no success. This was really frustrating because the FAQ for my hosting service said to change values in the .htaccess file as an alternative to making changes to the php.ini file.
The third solution is to add ini_set(‘memory_limit’, ‘32M’); to the top of each script that might require extra memory. Depending on how your scripts are setup, you might be able to get away with adding this to a header file or template file. It didn’t seem to work for me, until I added it to the upgrade.php file for WordPress. It’s not a very elegant solution, but it works. I actually added 64MB because I was frustrated.
I still don’t know why the second solution didn’t work, unless the server is ignoring that portion of the .htaccess file. I can’t say I’m 100% pleased about it, but if it happens in the future I will know what to do.
Related posts: