A detailed explanation of PHP's garbage collection mechanism

Source: Internet
Author: User
1) The garbage collection period for PHP is generated when the reference count is reduced to a value other than 0. Therefore, you first need to understand the reference count knowledge.

2) Each PHP variable exists in a variable container called "Zval". A Zval variable container that includes two bytes of extra information in addition to the type and value of the variable. The first is "Is_ref", which is a bool value that identifies whether the variable belongs to a reference collection (reference set).

3) The second extra byte is "RefCount", which represents the number of variables (also known as symbols) that point to the Zval variable container.

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

4) Test code example:

$str = ' Hello world! '; Xdebug_debug_zval (' str '); $STR 1 = $str 2 = $str; Xdebug_debug_zval (' str '); unset ($str 1); Xdebug_debug_zval (' str ');

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)

Recycling Cycle

1) The garbage cycle (garbage cycle) is generated when the reference count is reduced to a value other than 0. Second, in a garbage cycle, by checking whether the reference count is minus 1, and checking which variable containers have a reference number of zero, to find out which part is garbage.

2) PHP garbage collection algorithm is simulated delete, simulated recovery, really delete, each action is used deep search traversal.

3) The configuration of PHP recycle mechanism: ZEND.ENABLE_GC. It can also be wise to call the Gc_collect_cycles () function before invoking the gc_disable () function to open and close the garbage collection mechanism by calling the Gc_enable () and gc_disable () functions separately.

4) Areas of performance impact: The first is the memory footprint savings, and the other is the garbage collection mechanism execution time increases when performing memory cleanup

5) in PHP execution, garbage collection is performed once the root buffer is full or when the Gc_collect_cycles () function is called.

Related recommendations:

What is the PHP garbage collection mechanism

The evolution of garbage collection mechanism in analytic PHP5

Analysis of destructor __destruct and garbage collection mechanism in PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.