Home > Article > Backend Development > Detailed explanation of the garbage collection mechanism of PHP memory management (picture)
Each php variable exists in a zval variable container. In addition to the type and value of the variable, the container also contains whether is_ref belongs to the reference
also There is a refcount reference count. When assigning a variable to another variable, the number of references will be increased. When the variable is unset or leaves
its scope, the reference count is decremented by 1. When the reference count is reduced to 0 , memory recycling. However, such a mechanism has a memory leak when
loopingreferences.
Example: When an array itself is regarded as an array element, the following picture will appear:
When array a is unset After that, the problem appears:
#The memory cannot be recycled.
In order to solve this problem, the gc mechanism was introduced after php5.3. Specifically, it is to establish a root buffer and add suspicious zval variable containers to the
and buffer. The default is 1000, but Set, if it is full, it will be cleared according to the recycling algorithm mechanism.
The above is the detailed content of Detailed explanation of the garbage collection mechanism of PHP memory management (picture). For more information, please follow other related articles on the PHP Chinese website!