PHP garbage Collection

Source: Internet
Author: User
Example #1 generate a new Zval container

$a = "new string";
?>

In the example above, the new variable A is generated in the current scope. and a variable container of type string and a value of new string is generated. In the additional two bytes of information, "Is_ref" is set to FALSEby default because there are no custom references generated. "RefCount" is set to 1 because there is only one variable that uses this variable container. Notice that when the value of "RefCount" is 1 o'clock, the value of "Is_ref" is always FALSE. If you have installed»xdebug, you can display the values of "RefCount" and "Is_ref" by calling the function xdebug_debug_zval () .

Example #2 show zval information

Xdebug_debug_zval (' a ');
?>

The above routines will output:

A: (refcount=1, is_ref=0) = ' new String '

Assigning a variable to another variable increases the number of references (refcount).

Example #3 Add a zval reference count

$a = "new string";
$b = $a;
Xdebug_debug_zval (' a ');
?>

The above routines will output:

A: (refcount=2, is_ref=0) = ' new String '

At this point, the number of references is 2 because the same variable container is associated with variable A and variable B. When not necessary, PHP does not replicate the generated variable container. The variable container is destroyed when "RefCount" becomes 0 o'clock. When any variable associated to a variable container leaves its scope (for example, the function execution ends), or the function unset () is called on the variable, "RefCount" is reduced by 1, and the following example illustrates:

Example #4 Reduce reference count

$a = "new string";
$c = $b = $a;
Xdebug_debug_zval (' a ');
Unset ($b, $c);
Xdebug_debug_zval (' a ');
?>

The above routines will output:

A: (refcount=3, is_ref=0) = ' new String ' A: (Refcount=1, is_ref=0) = ' new String '

If we now execute unset ($a), the variable container containing the type and value is removed from memory.

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