Learning about the PHP garbage collection mechanism and understanding the concept of reference counters

Source: Internet
Author: User
Learning about the PHP garbage collection mechanism, understanding the concept of referencing counters. a php variable exists in a variable container named "zval". The "zval" variable container includes the types and values containing variables, it also includes two additional bytes of information, "is_ref" indicating whether the variable is referenced, and "refcount" indicates the number of variables in the zval variable container.

If you have installed xdebug, you can use xdebug_debug_zval () to display "zval" information. As follows:

   

  

Result:

Str:

(Refcount = 1, is_ref = 0 ),

String 'phpddt. com' (length = 10)

The variable container is destroyed only when "refcount" is changed to 0. when you unset () a variable, the refcount in the desired "zval" will be reduced by 1. let's talk about the unset reference problem encountered a few days ago:

    

  

Result:

B:

(Refcount = 1, is_ref = 0), string 'AAA' (length = 3)

Continue to talk about the reference counter problem. The matching types of array and object are different:

    'aaa', 'b' => "bbb" );xdebug_debug_zval( 'arr' );$arr['aaa'] = $arr['a'];xdebug_debug_zval( 'arr' );?>

  

Result:

Arr:

(Refcount = 1, is_ref = 0 ),

Array

'A' => (refcount = 1, is_ref = 0), string 'AAA' (length = 3)

'B' => (refcount = 1, is_ref = 0), string 'BBB '(length = 3)

Arr:

(Refcount = 1, is_ref = 0 ),

Array

'A' => (refcount = 2, is_ref = 0), string 'AAA' (length = 3)

'B' => (refcount = 1, is_ref = 0), string 'BBB '(length = 3)

'AAA' => (refcount = 2, is_ref = 0), string 'AAA' (length = 3)

We can see that the original array elements and newly added array elements are associated with the zval variable container of the same "refcount" 2. here I am only playing a role in attracting others.

For details about the PHP reference counter can refer to the manual: http://php.net/manual/zh/features.gc.refcounting-basics.php

Reprinted! Original address: http://www.phpddt.com/php/gc-refcounting-basics.html, reproduced please indicate the address, thank you!

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.