Alex Rivera | Logout

How do you debug php "Out of Memory" issues?

Asked 2011-05-24T17:07:46.267
39

I've had some issues lately with PHP memory limits lately:

Out of memory (allocated 22544384) (tried to allocate 232 bytes)

These are quite the nuisance to debug since I'm not left with a lot of info about what caused the issue.

Adding a shutdown function has helped

register_shutdown_function('shutdown');

then, using error_get_last(); I can obtain information about the last error, in this case, the "Out of memory" fatal error, such as the line number, and the php file name.

This is nice and all, but my php program is heavily object oriented. An error deep in the stack doesn't tell me much about the control structure or the execution stack at the moment of the error. I've tried debug_backtrace(), but that just shows me the stack during shutdown, not the stack at the time of the error.

I know I can just raise the memory limit using ini_set or modifying php.ini, but that doesn't get me any closer to actually figuring out what is consuming so much memory or what my execution flow looks like during the error.

Anyone have a good methodology for debugging memory errors in advanced Object Oriented PHP programs?

Edit
Report

2 Answers

12
echo '<pre>';
$vars = get_defined_vars();
foreach($vars as $name=>$var)
{
    echo '<strong>' . $name . '</strong>: ' . strlen(serialize($var)) . '<br />';
}
exit();

/* ... Code that triggers memory error ... */

I use this to print out a list of currently assigned variables just before a problem section of my code, along with a (very) rough estimate of the size of the variable. I go back and unset anything that isn't needed at and beyond the point of interest.

It's useful when installing an extension isn't an option.

You could modify the above code to use memory_get_usage in a way that will give you a different estimate of the memory in a variable, not sure whether it'd be better or worse.

answered 2013-03-27T04:22:13.257
5

I wonder is perhaps your thinking regards methodology is flawed here.

The basic answer to your question - how do I find out where this error is occurring? - has already been answered; you know what's causing that.

However, this is one of those cases where the triggering error isn't really the problem - certainly, that 232 byte object isn't your problem at all. It is the 20+Megs that was allocated before it.

There have been some ideas posted which can help you track that down; you really need to look "higher level" here, at the application architecture, and not just at individual functions.

It may be that your application requires more memory to do what it does, with the user load you have. Or it may be that there are some real memory hogs that are unnecessary - but you have to know what is necessary or not to answer that question.

That basically means going line-by-line, object-by-object, profiling as needed, until you find what you seek; big memory users. Note that there might not be one or two big items... if only it were so easy! Once you find the memory-hogs, you then have to figure out if they can be optimized. If not, then you need more memory.

answered 2011-05-24T17:31:15.670

Your Answer