PHP garbage collection mechanism

Source: Internet
Author: User

The entire reason for implementing the garbage collection mechanism is to conserve memory by cleaning up the variables referenced by the loop.

  1. Reference Count: PHP variable exists in a variable container called "Zval". A Zval variable container, in addition to the type and value of the variable, contains additional information of two bytes: Is_ref and RefCount. Is_ref is a bool value that identifies whether the variable is a reference set, and the PHP engine distinguishes between ordinary and reference variables, and refcount is used to represent the number of variables that point to the Zval variable container, when refcount = 0 means that the variable can be purged or recycled.
    <?php$a = Array (' meaning ' = ' life ', ' number '); $a [' life '] = $a [' meaning '];xdebug_debug_zval (' a ');? >

    The previous routine output is as follows:

    A: (Refcount=1, Is_ref=0) =array (   ' meaning ' = (refcount=2, is_ref=0) = ' life ', ' number ' = = (Refcount=1, is   _ref=0) =42,   ' life ' (refcount=2, is_ref=0) = ' life ')

    The output of the Xdebug is shown as two zval variable containers with a value of ' life ' , which is actually the same:

    <?php$a = Array (' one '), $a [] =& $a; Xdebug_debug_zval (' a '); unset ($a); Xdebug_debug_zval (' a ');? >

    The results of the above example are as follows:

    (Refcount=1, Is_ref=1) =array (   0 = (refcount=1, is_ref=0) = ' One ',   1 = (refcount=1, is_ref=1) = ...)

    The previous example, although no longer has any symbols in a scope pointing to the variable container, the array element "1" still points to the array itself, so the container cannot be cleared. Because there is no additional symbol pointing to it, the user has no way to clear the structure, resulting in a memory leak. PHP will erase this data structure at the end of the script execution, but it will consume a lot of memory before PHP clears.

  2. Recycle cycle : Can be used to handle circular references causing a memory leak issue. If a variable container reference count increases, it will continue to be used, of course it is no longer in garbage, and if the reference count is reduced to zero, the variable container will be cleared (free). That is, the garbage cycle (garbage cycle) is generated only when the reference count is reduced to a value other than 0, and secondly, in a garbage cycle, by checking whether the reference count is reduced by 1 (impersonation), and by checking which variable containers have a reference number of zero, to find out which part is garbage
  3. turn garbage collection on and off : In addition to modifying configuration zend.enable_gc, the garbage collection mechanism can be turned on and off by calling the Gc_enable () and gc_disable () functions respectively. In addition, even if the garbage collection mechanism is not available, it is possible that the root is also logged so that every time a possible root is found, the garbage collection mechanism is not checked for opening, the logging operation is faster, and the call to the Gc_collect_cycles () function enforces cycle recycling.

PHP garbage collection mechanism

Related Article

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.