Php garbage collection

Source: Internet
Author: User
Php garbage collection Example #1 generate a new zval container

$ A = "new string ";
?>

In the preceding example, the new variable a is generated in the current scope. A variable container of the string type and the new string value is generated. In the additional two bytes, "is_ref" is setFALSEBecause no custom reference is generated. "Refcount" is set to 1 because only one variable uses this variable container. Note that when the value of "refcount" is 1, the value of "is_ref" is alwaysFALSE. If you have installed» Xdebug, you can call the functionXdebug_debug_zval ()Displays the values of "refcount" and "is_ref.

Example #2 display zval information

Xdebug_debug_zval ('A ');
?>

The above routine will output:

a: (refcount=1, is_ref=0)='new string'

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

Example #3 add a zval reference count

$ A = "new string ";
$ B = $;
Xdebug_debug_zval ('A ');
?>

The above routine will output:

a: (refcount=2, is_ref=0)='new string'

At this time, the number of references is 2, because the same variable container is associated with variable a and variable B. php does not copy the generated variable container when it is not necessary. The variable container is destroyed when "refcount" is changed to 0. when any variable associated with a variable container leaves its scope (for example, function execution ends), or the function unset () is called for the variable, "refcount" is reduced by 1, the following example shows how:

Example #4 reduce reference count

$ A = "new string ";
$ C = $ B = $;
Xdebug_debug_zval ('A ');
Unset ($ B, $ c );
Xdebug_debug_zval ('A ');
?>

The above routine will output:

a: (refcount=3, is_ref=0)='new string'a: (refcount=1, is_ref=0)='new string'

If we execute unset ($ a);, the variable container containing the type and value will be deleted from the 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.