Will memory be clogged?

public function recursive(&$a) { if ($a < 100000000000) { $a++; $this->recursive($a); } } 

////// -------------------------------------------- ------- ////////

 $a = 1; $test = new MyClass(); $test->recursive($a); 
  • Why not just read the manual ? - hindmost
  • @hindmost And what does it say? If you had quoted from this link, which answers the question, it would be much more informative. - jekaby

1 answer 1

The value is passed by reference, therefore, the memory will not be allocated for each operation. This is in theory.

 $m1 = memory_get_usage(true); $a = 1; $test = new MyClass(); $test->recursive($a); $m2 = memory_get_usage(true); var_dump($m2 - $m1); // 0 var_dump(memory_get_usage()); 

On the virtual machine and on my machine checked. Regardless of the size of the memory cycle, it is always equally allocated ( if you believe php ).

But if you look at the htop at the virtual machine at this time, then the memory is eaten up very noticeably ... At 20M iterations, the virtual machine does hang at all:

htop utility output - all memory eaten

And, if you watch htop not on a virtual machine, but on a main machine, then the memory consumption is not noticeable. True, I have another problem, if you put the number of iterations close to 100k, then it falls with an error:

Segmentation error (memory dump made)

All tested through cli.

  • thank you so much - Serge Esmanovich