Originally Published 2007-09-17 15:26:54
I did this on a client's server late last night. I got it sorted in less than 20 minutes (would've been 2 with shell access, but I had to call the hosting provider's support line), but it was still a pretty frantic time frame.
- Construct your site to make extensive use of PHP so that the interpreter is required to render every single page of your site. This is remarkably easy (and common) if the first line of every page is session_start(), include("my_header.inc.php") or similar.
- Leave your PHP memory cache settings at the default of 8 megabytes (8M) in php.ini. The configuration directive is "memory_limit".
- Construct a database query that has more joins than the number of times Lindsey Lohan has been to rehab. Store the result set in a PHP variable.
- When you get an out of memory error -- something like "Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 346 bytes) in /home/user/public_html/your_script.php on line 23" -- ask google, get advice to arbitrarily raise your local script's memory limit above the default using ini_set('memory_limit', '256M') and try again.
- After still not getting the page to render correctly, manually set your timeout to some arbitrarily high value (like 5 minutes, in my case) using set_time_limit(300).
- Try to pull up your index page. Every single page on your site that uses PHP will be throwing the out of memory error outlined in #4, above.
So why does this happen?
Your php.ini file controls the total memory available to the PHP interpreter. So if it's set to 8M (8 megabytes is 8388608 bytes, i.e., 8 * 1024 *1024) and you arbitrarily run a script that has the ceiling lifted, then let that script consume more than the 8M and hold onto it via the increased timeout period, the cache will always be over the 8 megabyte ceiling. This means when your normal pages try to allocate some memory, PHP returns an error saying it's all used up. And you, my friend, have broken site.
Oh Crap. I did this, because I'm not a regular subscriber to Really Smart Guy. How do I fix it?
Well, get with the program! Subscribe to my RSS Feed.
The simplest thing to do is probably restart apache if you have sufficient access and are running on a dedicated server. If you're not on a dedicated box, than you'll need to clear the php cache.Â On my server, that's in /home/user/tmp, but it depends on your hosting provider.
Unfortunately, if you don't have shell access, and don't at least have a virtual private server (i.e., you're on shared hosting, which is likely if you're just a blogger and/or are handling less than 10K unique visitors a month), you're probably stuck calling tech support, which means you're going to be down for at least another 20 minutes. Sorry.
Your best bet is to not increase your memory allocation on a script basis. In fact, to help sandbox your development scripts (aside from running them on server that is not in production, of course, which is your best bet if you have the budget for it),Â you should set your script based memory allocation lower than the global php.ini setting. So if your global allocation is, say, 256M, then set your local script malloc to 16, and you can have the thing runaway 16 times before you break your sandbox. Make sense? That even allows some minimal toying with your timeouts. ;-)
What the heck? Isn't this blog supposed to be about business, real estate, and making money?
Yeah, it's a mixed bag. These techie posts actually get a lot of long tail google action, which is a subtle business tip in and of itself.
Nevertheless, there's still lots of archive love for you. See the entrepreneurship and real estate archives. Or just ask me something.