Detailed explanation of the garbage collection mechanism of PHP memory management (picture)

黄舟
Release: 2023-03-06 19:22:02
Original
3086 people have browsed it

Eachphp variableexists in a zval variable container. In addition to the type and value of the variable, the container also contains whether is_ref belongs to thereference

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 memoryleak when

looping

references.

Example: When anarrayitself is regarded as an array element, the following picture will appear:

Detailed explanation of the garbage collection mechanism of PHP memory management (picture)

When array a is unset After that, the problem appears:

Detailed explanation of the garbage collection mechanism of PHP memory management (picture)

#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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!