PHP garbage Collection Mechanism reference counter concept analysis _php tips

Source: Internet
Author: User
Tags garbage collection

If you have Xdebug installed, you can use Xdebug_debug_zval () to display "Zval" information. As follows:

Copy Code code as follows:

<?php
$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 the "RefCount" becomes 0 o'clock. When you unset () a variable, the refcount of the desired "Zval" is reduced by 1, and the unset reference problem encountered a few days ago:

Copy Code code as follows:

<?php
$a = "AAA";
$b = & $a;
unset ($a);
Echo $b; This will still output AAA, print with Xdebug_debug_zval you'll know why.
Xdebug_debug_zval ("B");

Results:

B:
(Refcount=1, is_ref=0), String ' aaa ' (length=3)
Continue to refer to counter issues, which are different for array and object conformance types:

Copy Code code as follows:

<?php
$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 with the Zval variable container of the same "RefCount" 2. I'm just playing a role here.

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

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.