PHP garbage Collection Mechanism Reference counter conceptual analysis _php Tutorial

Source: Internet
Author: User
If you install Xdebug, you can use Xdebug_debug_zval () to display the "Zval" message. As follows:

Copy the Code code as follows:
$str = "Jb51.net";
Xdebug_debug_zval (' str ');

Results:

Str:
(Refcount=1, is_ref=0),
String ' jb51.net ' (length=10)

The variable container is destroyed only when "RefCount" becomes 0 o'clock. When you unset () a variable, the refcount in the desired "zval" will be reduced by 1, then the unset reference problem encountered in the previous days:

Copy the Code code as follows:
$a = "AAA";
$b = & $a;
unset ($a);
Echo $b; This will still output AAA, print with Xdebug_debug_zval and you'll know why.
Xdebug_debug_zval ("B");

Results:

B:
(Refcount=1, is_ref=0), String ' aaa ' (length=3)
Continue to refer to the counter problem, which is different for array and object conforming types:

Copy the Code code as follows:
$arr = Array (' a ' = = ' aaa ', ' b ' = ' BBB ');
Xdebug_debug_zval (' arr ');
$arr [' aaa '] = $arr [' a '];
Xdebug_debug_zval (' arr ');
?>

Results:

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)

You can see that the original array element and the newly added array element are associated to the Zval variable container of the same "RefCount" 2. I'm just playing a role here.

Specific reference counters for PHP can be referenced in the manual: http://php.net/manual/zh/features.gc.refcounting-basics.php

http://www.bkjia.com/PHPjc/327870.html www.bkjia.com true http://www.bkjia.com/PHPjc/327870.html techarticle If you install Xdebug, you can use Xdebug_debug_zval () to display the "Zval" message. As follows: Copy code code as follows: PHP $str = "Jb51.net"; Xdebug_debug_zval (' str '); Result: .....

  • 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.