title: How to free memory after array in PHP? unset and = null seems to not working
body: <p>I ve got a 16 MB size CSV file and try to parse it, and do some stuff, but script run out of memory after some time. I realize that this code generate around 200 MB of used space and unset is not working.</p>
  
  <pre><code>    $countRows = 1;
      var_dump("3. ".memory_get_usage()." beginDiff: ".(memory_get_usage() - $this-&gt;startingMemory));
      while(($row = fgetcsv($fp, 300000, ';', '"')) !== FALSE)
      {
          if ($row == '')
              continue;
  
          if($firstRow == true)
          {
              foreach($row as $k =&gt; $v)
              {
                  $this-&gt;columnMapping[$k] = trim(mb_strtolower($v));
              }
              $firstRow = false;
              continue;
          }else
          {
              foreach($row as $k =&gt; $v)
              {
                  $row[$this-&gt;columnMapping[$k]] = $v;
                  unset($row[$k]);
              }
          }
      ...
          //$this-&gt;theirCategoriesToProducts[$row['kategorie']][]['kodproduktu'] = $row['kodproduktu'];
          $this-&gt;theirCategoriesToProducts[$row['kategorie']][] = $row;
      }
      var_dump("3,5.  ".memory_get_usage()." beginDiff: ".(memory_get_usage() - $this-&gt;startingMemory));
      ...
      var_dump("7. - before unset total: ".memory_get_usage()." beginDiff: ".(memory_get_usage() - $this-&gt;startingMemory));
      unset($this-&gt;theirCategoriesToProducts);
      var_dump("8. - after unset total: ".memory_get_usage()." beginDiff: ".(memory_get_usage() - $this-&gt;startingMemory));die;
  </code></pre>
  
  <p>Generating this output:</p>
  
  <pre><code>    string '3. 72417440 beginDiff: 34730040' (length=31)
      string '3,5.  292748528 beginDiff: 255061136' (length=36)
      string '7. - before unset total: 299039360 beginDiff: 261351984' (length=55)
      string '8. - after unset total: 297364432 beginDiff: 259677056' (length=54)
  </code></pre>
  
  <p>With setting that variable equals null is the output very similar. But switching comments between this two lines</p>
  
  <pre><code>    $this-&gt;theirCategoriesToProducts[$row['kategorie']][]['kodproduktu'] = $row['kodproduktu'];
      //$this-&gt;theirCategoriesToProducts[$row['kategorie']][] = $row;
  </code></pre>
  
  <p>Will output:</p>
  
  <pre><code>    string '3. 72417784 beginDiff: 34730040' (length=31)
      string '3,5.  81081984 beginDiff: 43394248' (length=34)
      string '7. - before unset total: 87256544 beginDiff: 49568824' (length=53)
      string '8. - after unset total: 85581520 beginDiff: 47893800' (length=52)
  </code></pre>
  
  <p>So its about 200 MB of "lost" memory (almost half of dedicated). </p>
  
  <p>Recursive function unseting all parts of arrays eat more memory, than was able to free so crashed also.</p>
  
  <p>In script is never used that array with &amp; so there should be no references to other variables.</p>
  
  <p>File is closing right after 3.5 dump.</p>
  
  <p>Any other ideas, how to unset that array?</p>