title: Tools/Techniques for Debugging Memory Leaks/Usage Bugs in PHP
body: <p>I've got a command line script that is running an <code>array()</code> of files through a loop, and using the file name as an argument to call a method on a helper object.</p>
  
  <p>Each run through the script, PHP's <code>memory_get_usage</code> reports a larger and larger number</p>
  
  <pre><code>53294264
  57019624
  61374624
  65699176
  70230600
  75157152
  79900392
  84630472
  89359264
  94300016
  100031176
  105202448
  110360808
  115777528
  121146976
  126784824
  132366952
  </code></pre>
  
  <p>until I error out with a</p>
  
  <pre><code>PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted
  </code></pre>
  
  <p>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?   </p>
  
  <p>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.</p>