PHP garbage collection mechanism reference counter concept analysis_PHP tutorial

WBOY
Release: 2016-07-21 15:03:04
Original
823 people have browsed it

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

Copy code The code is as follows:

$str = "jb51.net";
xdebug_debug_zval('str');

Result:

str:
(refcount=1, is_ref=0),
string 'jb51.net' (length=10)

Only when the variable container is destroyed when "refcount" becomes 0. When you unset() a variable, the refcount in the desired "zval" will be reduced by 1. Let's talk about what we encountered a few days ago. unset reference problem:

Copy code The code is as follows:

$a = "aaa";
$ b = & $a;
unset($a);
//echo $b; //aaa will still be output here, you will know why if you print it with xdebug_debug_zval
xdebug_debug_zval("b");

Result:

b:
(refcount=1, is_ref=0), string 'aaa' (length=3)
Continuing to talk about the reference counter issue, the situation is different for array and object types:

Copy code The code is as follows:

$arr = array( 'a' => '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)

You can see that the original array elements and the newly added array elements are associated with the same zval variable container of "refcount" 2. Here I am just trying to inspire others.

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327870.htmlTechArticleIf you have xdebug installed, you can use xdebug_debug_zval() to display "zval" information. As follows: Copy the code The code is as follows: ?php $str = "jb51.net"; xdebug_debug_zval('str'); Result:...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template