Are PHP Closure Objects Suitable For Garbage Collection

I was wondering if anyone knows if anonymous PHP functions can have garbage collection right?

I know that functions created using create_function are not garbage collected, but I could not find a link to those created with function(){} syntax (internally represented as a Closure object).

+4
source share
2 answers

The PHP garbage collector does not distinguish between the types of β€œthings” - if it has at least one link somewhere, it is saved. At a time when this is not applied, the resource collects garbage.

This is not the same as using create_function , as PHP adds the create_function link to the global scope in addition to the link to it. Closing (the Closure object, if you prefer, because that's what they are!) Exists only in the area in which it was created in + of all those with which you transmit.

If you want to make sure of this, run this little piece of code:

 <?php $r = memory_get_usage(); for ($i = 0; $i < 100; $i++) { $k = function() {echo "boo"; }; if (memory_get_usage() > $r) { echo "Different memory count. Off by: ".(memory_get_usage() -$r); } $r = memory_get_usage(); } 

You will receive exactly one echo. Replace the assignment of $k with create_function and you will get 100.

+10
source

You can see xdebug_debug_zval ('a'); if xdebug is installed. http://www.php.net/manual/en/features.gc.refcounting-basics.php

0
source

Source: https://habr.com/ru/post/1481454/


All Articles