4

I've got a command line script that is running an array() of files through a loop, and using the file name as an argument to call a method on a helper object.

Each run through the script, PHP's memory_get_usage reports a larger and larger number

53294264
57019624
61374624
65699176
70230600
75157152
79900392
84630472
89359264
94300016
100031176
105202448
110360808
115777528
121146976
126784824
132366952

until I error out with a

PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted

It's clear that something is leaking memory, or some sort of global/static storage area keeps being added to. What's the best way to debug this kind of error in PHP? Are there common situations where this occurs inadvertently in PHP? Does xDebug have some magic options that can help me pinpoint this? Other tools?

The best I've come up with is following the chain of execution and littering the codebase with calls to memory_get_usage() until I pinpoint the exact problem, but that seems like a tedious and inefficient approach.

Alana Storm
  • 164,128
  • 91
  • 395
  • 599
  • 2
    I had a similar question http://stackoverflow.com/questions/849549/finding-cause-of-memory-leaks-in-large-php-stacks. Unfortunately it didn't yield any 'miracle' solutions. – Mike B Jul 28 '10 at 01:39
  • Useful info though Mike, thanks for the pointer. – Alana Storm Jul 28 '10 at 02:16
  • Workaround for those interested. I ended up writing a bash wrapper for my script. The script itself checks the current memory usage (http://us3.php.net/memory_Get_usage) vs. the memory limit (http://us3.php.net/manual/en/ini.core.php#ini.memory-limit) and bails with a specific exit code (exit(1)) when we start running out. If the bash script detects that exit code, it re spawns itself. The object that's being looped over is built by reading in a list of IDs from an external source, and this list is updated each time the script makes it through the loop successfully. – Alana Storm Jul 29 '10 at 17:01
  • I take it that xhprof wasn't much help in this situation? – Charles Jul 29 '10 at 17:57
  • @Charles - This was for a onetime import script, It was quicker to code up a simple workaround than it was to get a new PECL extension up and running in my environment and learning to read its output. I'm planning to checkout xhprof in the future though! – Alana Storm Jul 29 '10 at 18:10

1 Answers1

5

It looks like Facebook's xhprof provides memory profiling at the function call level.

(I have never used it, but really want to...)

Charles
  • 50,943
  • 13
  • 104
  • 142