When trying to trace some memory issues in PHP, I noticed that debug_backtrace(), which I call in my logging code, seemed to be using a lot of memory.
In most cases, the following code prints something like 0.02 MB. But in one case, it prints 171.85 MB!
$before = memory_get_usage();
$backtrace = debug_backtrace(false);
$after = memory_get_usage();
echo round(($after - $before)/1024/1024, 2)." MB";
My question is, does this mean that debug_backtrace is actually using that much memory? Or could something else be happening, like garbage collection, that messes up the return value from memory_get_usage?
Okay, I think I figured it out. I printed out the backtrace, and the
"args"array was huge. This is because I was passing around some enormous strings. I guess it’s making copies of them (instead of references) when returning the results.For example:
Try this with and without the debug_backtrace() call. With it, the memory usage is increased by about 28 MB. It’s only cleared when test3() returns.