title: PHP Memory Allocation and Deallocation
body: <p>So, I am running a long-running script that is dealing with memory sensitive data (large amounts of it). I (think) I am doing a good job of properly destroying large objects throughout the long running process, to save memory. </p>
  
  <p>I have a log that continuously outputs current memory usage (using memory_get_usage()), and I do not notice rises and drops (significant ones) in memory usage. Which tells me I am probably doing the right thing with memory management. </p>
  
  <p>However, if I log on to the server and run a top command, I notice that the apache process that is dealing with this script never deallocates memory (at least visibly though the top command). It simply remains at the highest memory usage, even if the current memory usage reported by php is much, much lower.</p>
  
  <p>So, my question is: are my attempts to save memory futile if the memory isnt really being freed back to the server? Or am I missing something here.</p>
  
  <p>Thank you.</p>
  
  <p>ps. using php 5.4 on linux</p>
  
  <p>pps. For those who want code, this is a basic representation: </p>
  
  <pre><code>function bigData()
  {
      $obj = new BigDataObj();
      $obj-&gt;loadALotOfData();
  
      $varA = $obj-&gt;getALotOfData();
  
      //all done
      $obj = NULL;
      $varA = NULL;
      unset($obj,$varA);
  }
  </code></pre>
  
  <p>update: as hek2mgl recommended, I ran debug_zval_dump(), and the output, to me, seems correct.</p>
  
  <pre><code>function bigData()
  {
      $obj = new BigDataObj();
      $obj-&gt;loadALotOfData();
  
      //all done
      $obj = NULL;
  
      debug_zval_dump($obj);
  
      unset($obj);
  
      debug_zval_dump($obj);
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>NULL refcount(2)
  
  NULL refcount(1)
  </code></pre>