Home > Backend Development > PHP Tutorial > Detailed explanation of PHP's garbage collection mechanism

Detailed explanation of PHP's garbage collection mechanism

小云云
Release: 2023-03-21 10:04:02
Original
2069 people have browsed it

1) PHP's garbage collection cycle is generated when the reference count is reduced to a non-zero value. Therefore, you first need to understand reference counting.

2) Each php variable exists in a variable container called "zval". A zval variable container contains, in addition to the type and value of the variable, two bytes of additional information. The first one is "is_ref", which is a bool value used to identify whether this variable belongs to the reference set.

3) The second extra byte is "refcount", which is used to indicate the number of variables (also called symbols) pointing to this zval variable container.

5) Assigning a variable to another variable will increase the number of references (refcount). When any variable associated with a variable container leaves its scope (for example, the function execution ends), or the function unset() is called on the variable, the "refcount" will be decremented by 1.

4) Test code example:

$str = 'Hello World!';xdebug_debug_zval('str');$str1 = $str2 = $str;xdebug_debug_zval('str');unset($str1);xdebug_debug_zval('str');
Copy after login

The above example output

str:(refcount=1, is_ref=0),string 'Hello World!' (length=12)str:(refcount=3, is_ref=0),string 'Hello World!' (length=12)str:(refcount=2, is_ref=0),string 'Hello World!' (length=12)
Copy after login

Recycle cycle

1) Only when the reference count is reduced to a non-zero value, A garbage cycle will occur. Secondly, during a garbage cycle, find out which parts are garbage by checking whether the reference count is reduced by 1 and checking which variable containers have zero reference times.

2) The PHP garbage collection algorithm is simulated deletion, simulated recovery, and real deletion. Each action uses deep search traversal.

3) Configuration of PHP recycling mechanism: zend.enable_gc. The garbage collection mechanism can also be turned on and off by calling the gc_enable() and gc_disable() functions respectively. It may be wise to call the gc_collect_cycles() function before calling the gc_disable() function to release memory.

4) Areas of impact on performance: The first is the saving of memory space, and the other is the increase in execution time when the garbage collection mechanism performs memory cleanup

5) During PHP execution , once the root buffer is full or the gc_collect_cycles() function is called, garbage collection will be performed.

Related recommendations:

What is the PHP garbage collection mechanism

Analysis of the evolution of the garbage collection mechanism in PHP5

Analysis of destructor __destruct and garbage collection mechanism in php

The above is the detailed content of Detailed explanation of PHP's garbage collection mechanism. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template