PHP: Array of objects and memory leak -


i have problem memory leak problem during export of large number of files arrays of objects. simplified code looks this:

class test_class {     private $a = null;      public function __construct($a = null)     {         $this->a = $a;     }      public function __destruct()     {         unset($this->a);     } }  print 'memory before: '.memory_get_usage(1).' <br>'; // 262 144  $a = []; ($i=0; $i<600000; $i++)     $a[] = new test_class($i);  print 'memory after create: '.memory_get_usage(1).' <br>'; // 129 761 280   for($i=0; $i < count($a); $i++)     unset($a[$i]);  unset($a);  print 'memory after: '.memory_get_usage(1).' <br>'; // 35 389 440  

at 1 of next iterations memory still ends. idea how free memory occupied?

p.s. try unset()/assignment null , gc_collect_cycles(), none of methods has allowed me release memory occupied array of objects

i don't think memory leak. memory freed , available, seems php keep use , don't give system. guess has garbage collector. seems bad behavior maybe there reason...

her proof: (because of configuration, used smaller values behavior same)

/*    class definition */  print 'memory before: '.memory_get_usage(1).' <br>'; // 262 144  $a = []; $b = []; ($i=0; $i<5000; $i++)     $a[] = new test_class($i);  print 'memory after create: '.memory_get_usage(1).' <br>'; // 2 359 296   for($i=0; $i < count($a); $i++)     unset($a[$i]);  unset($a);  print 'memory after unset: '.memory_get_usage(1).' <br>'; // 1 572 864   ($i=0; $i<1000; $i++)     $b[] = $i;  print 'memory after $b: '.memory_get_usage(1).' <br>'; // 1 572 864  

you can see here creation of $b didn't need more memory. odd because array needs memory when don't before:

$b = [];  print 'memory before: '.memory_get_usage(1).' <br>'; // 262 144   ($i=0; $i<1000; $i++)     $b[] = $i;  print 'memory after: '.memory_get_usage(1).' <br>'; // 524 288  

that why think memory freed php sits on it.


Comments