title: Track memory usage of a method
body: <p>I have yet to find an elegant solution for this. I have a class with a method I want to track the memory usage of without modifying the function:</p>
  
  <pre><code>class Example
  {
      public function hello($name)
      {
          $something = str_repeat($name, pow(1024, 2));
      }
  }
  
  $class = new Example;
  $class-&gt;hello('a');
  </code></pre>
  
  <p>So the task is, how much memory does <code>hello()</code> use without interferring with it?</p>
  
  <p><strong>Note:</strong> The memory usage of this method should be 1MB. I've tried wrapping the call with <code>memory_get_usage();</code> to no avail:</p>
  
  <pre><code>class Example
  {
      public function hello($name)
      {
          $something = str_repeat($name, pow(1024, 2));
      }
  }
  
  $class = new Example;
  
  $s = memory_get_usage();
  
  $class-&gt;hello('a');
  
  echo memory_get_usage() - $s;
  </code></pre>
  
  <p>This only results in <code>144</code> bytes (Not correct at all). I've tried various magic with Reflection by using the <code>ReflectionMethod</code> class.</p>
  
  <p>I have a feeling what I need to do is calculate the difference in the method :(. If someone can think of anything cleaner then you'd really make my day.</p>
  
  <p><strong>Edit:</strong> I should mention this is in the context of a benchmarking application. So while <code>memory_get_peak_usage</code> works in the sense that it correctly returns the memory usage, it will also skew benchmarks ran after  a high memory method. Now if there was a way to reset the memory stats, that could be good also.</p>