PHP can automatically manage the memory and clear unnecessary objects. PHP uses the referencecounting (garbagecollection) mechanism
PHP can automatically perform memory management to clear unnecessary objects. PHP uses reference counting, a simple garbage collection mechanism. each object contains a reference counter. each reference is connected to an object, and the counter is incremented by 1. when the reference leaves the living space or is set to NULL, the counter minus 1. when the reference counter of an object is zero, PHP knows that you no longer need to use this object, releasing the memory space occupied by it.
The garbage collection mechanism used before php 5.3 is a simple "reference count", that is, each memory object is allocated with a counter. when the memory object is referenced by a variable, the counter is + 1; when the variable reference is removed, counter-1; when counter = 0, indicates that the memory object is not used, the memory object is destroyed, and garbage collection is completed.
There is a problem with "reference count", that is, when two or more objects reference each other to form a ring, the counter of the memory object will not be reduced to 0; at this time, this group of memory objects is useless, but cannot be recycled, resulting in memory leakage. php5.3 started with the new garbage collection mechanism, based on the reference count, a complex algorithm is implemented to detect the existence of reference rings in memory objects to avoid memory leakage.
The php variable exists in a variable container named "zval". The "zval" variable container includes the variable type and value, and contains two additional bytes, "is_ref" indicates whether the variable is referenced, and "refcount" indicates the number of variables in the zval variable container.
If you have installed xdebug, you can use xdebug_debug_zval () to display "zval" information as follows:
-