PHP garbage collection mechanism-basic knowledge of reference counting

Source: Internet
Author: User
Tags scalar
Each PHP variable exists in a variable container called "Zval". A Zval variable container that includes two bytes of extra information in addition to the type and value of the variable. The first is "Is_ref", which is a bool value that identifies whether the variable belongs to a reference collection (reference set). With this byte, the PHP engine can differentiate between normal and reference variables, and since PHP allows users to use the custom reference by using &, there is an internal reference counting mechanism in the Zval variable container to optimize memory usage. The second extra byte is "RefCount", which represents the number of variables (also known as symbols) that point to the Zval variable container. All symbols exist in a symbol table, where each symbol has scope (scope), and those main scripts (for example, scripts that are requested by the browser) and each function or method also have scopes.

When a variable is assigned a constant value, a Zval variable container is generated, as in the following example:

Example #1 Create a new Zval container

<?php    $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 FALSE by 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

<?php    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).

The growth of RefCount in Example #3 zval

<?php    $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:

Reduction of RefCount in Example #4 zval

<?php    $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.

Composite type (Compound Types)

When considering composite types such as array and object, things are a little bit more complicated. Unlike scalar (scalar) type values, variables of array and type object have their members or properties present in their own symbol table. This means that the following example will generate three Zval variable containers.

Example #5 Create an array zval

<?php    $a = array (' meaning ' = = ' life ', ' number ' =);    Xdebug_debug_zval (' a ');? >

The output of the above routines is similar to the following:

A: (Refcount=1, Is_ref=0) =array (   ' meaning ' = (refcount=1, is_ref=0) = ' life ', ' number ' = = (Refcount=1, is   _ref=0) =42)

Example #6 Add an existing element to the array

<?php    $a = array (' meaning ' = = ' life ', ' number ' =);    $a [' life '] = $a [' meaning '];    Xdebug_debug_zval (' a ');? >

The output of the above routines is similar to the following:

A: (Refcount=1, Is_ref=0) =array (   ' meaning ' = (refcount=2, is_ref=0) = ' life ', ' number ' = = (Refcount=1, is   _ref=0) =42,   ' life ' (refcount=2, is_ref=0) = ' life ')

or graphical display as follows:

from the Xdebug output information above, we see that the original array element and the newly added array element are associated to the Zval variable container of the same "RefCount" 2. Even though the output of Xdebug shows two zval variable containers with a value of ' life ', it is actually the same. The function xdebug_debug_zval () does not display this information, but you can see it by displaying the memory pointer information.

Deleting an element in an array is similar to removing a variable from the scope. After deletion, the "refcount" value of the container in the array is reduced, similarly, when "RefCount" is 0 o'clock, the variable container is removed from memory, and another example can be described as follows:

Example #7 removing elements from an array

<?php    $a = array (' meaning ' = = ' life ', ' number ' =);    $a [' life '] = $a [' meaning '];    unset ($a [' meaning '], $a [' number '];    Xdebug_debug_zval (' a ');? >

The output of the above routines is similar to the following:

A: (Refcount=1, Is_ref=0) =array (   ' life ' = [Refcount=1, is_ref=0) = ' life ')

Now, when we add an array itself as an element of this array, things become interesting, and the next example illustrates this. In the example we added the reference operator, otherwise PHP will generate a copy.

Example #8 Adding array elements to the array itself

<?php    $a = array (' one ');    $a [] =& $a;    Xdebug_debug_zval (' a ');? >

The output of the above routines is similar to the following:

A: (refcount=2, Is_ref=1) =array (   0 = (refcount=1, is_ref=0) = ' One ',   1 = (refcount=2, is_ref=1) = ...)

Or graphically

You can see that the array variable (a) is also the second element (1) of the array that points to the variable container where "RefCount" is 2. The "..." in the above output indicates that a recursive operation has occurred, obviously in this case means "..." pointing to the original array.

Just like that, calling unset on a variable will delete the symbol and the number of references in the variable container it points to is also reduced by 1. So, if we call unset on the variable $ A after executing the above code, the variable $a and array element "1" points to a variable container that has a reference count minus 1, from "2" to "1". The following example illustrates:

Example #9 Destruction $a

(Refcount=1, Is_ref=1) =array (   0 = (refcount=1, is_ref=0) = ' One ',   1 = (refcount=1, is_ref=1) = ...)

or graphical display as follows:

issues with cleaning up variable containers (Cleanup problems)

Although there is no longer any symbol in a scope pointing to the structure (that is, the variable container), the array element "1" still points to the array itself, so the container cannot be cleared. Because there is no additional symbol pointing to it, the user has no way to clear the structure, resulting in a memory leak. Fortunately, PHP will erase this data structure at the end of the request, but it will consume a lot of memory before PHP clears. This happens frequently if you want to implement an analytic algorithm, or if you want to do something like a child element that points to its parent element. Of course, the same is true for objects, which are actually more likely to occur because objects are always implicitly referenced.

If the situation above is nothing but one or two times, it is obviously a big problem if there are thousands of or even hundreds of thousands of memory leaks. In a long-running script, such as a daemon (deamons) that does not essentially end the request, or a large suite in a unit test (sets), the latter (referred to as the large suite in the unit test) when doing unit testing on the template component of the EZ component library There will be problems. It will need to consume 2GB of memory, while the general test server does not have that much memory space.

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