Understanding the garbage collection mechanism of PHP

Source: Internet
Author: User
Tags gc collect zend

What is a garbage collection mechanism

Use a "reference count" method for recycling. Simply understood, there is a counter in each allocated memory area that records how many variable pointers point to this piece of memory. When the number of pointers to this slice of memory is 0, the memory area of the slice can be reclaimed.

What's rubbish?

First we need to define the concept of "garbage", saying that the simple point is that the container of the variable zval still exists, but there is no variable name pointing to this zval. An important criterion for judging whether or not to be garbage is that there is no variable name pointing to the variable container zval.

Suppose we have a PHP code, using a temporary variable $tmp stored a string, after processing the string, we do not need the $TMP variable, $tmp variable for us can be considered a "garbage", but $tmp is not a garbage,

$tmp variable does not make sense to us, but the variable actually exists, and the $tmp symbol still points to its corresponding zval,php code may also use this variable, so it is not defined as garbage.

So if we use the PHP code to finish $tmp, call unset Delete this variable, then $tmp is not become a garbage it? Don't worry about answering this question first. Let's give an example:

<= Array ('one'= &$a; unset ($a);?

The $ A array has two elements, an index of 0, a character one, an index of 1, and a reference to $ A itself, which is stored internally as follows:

A: (refcount=2, is_ref=1) =Array (   0 = (refcount=1, is_ref= 0) ='one',   1 = (refcount=2, is_ref=1 ) =...)

"..." means that 1 points to a itself, which is a circular reference:

Finally we unset a $ A, then $ A will be removed from the symbol table, and the refcount of $ A points zval reduced by 1

Then the problem arises, $a is no longer in the symbol table, and the user cannot access the variable again, but $ A refcount of the zval pointed to is 1 instead of 0 and therefore cannot be reclaimed, resulting in a memory leak:

In this way, such a zval becomes a really meaningful rubbish, and the new GC's job is to clean up the rubbish.

To solve this kind of rubbish, a new GC was created

in the PHP5.3 version, using a special GC mechanism to clean up the garbage, in the previous version there is no special GC, then garbage generation, there is no way to clean up, memory wasted. In the PHP5.3 source code, the following files: {phpsrc}/zend/zend_gc.h {phpsrc}/zend/zend_gc.c, here is the implementation of the new GC, we first briefly introduce the idea of the algorithm, Then from the source point of view in detail how the engine implementation of this algorithm.

A new GC algorithm

in the newer PHP manual, there is a simple introduction to the garbage cleanup algorithm used by the new GC, called Concurrent Cycle Collection in Reference counted Systems, which is not described in detail here, according to The contents of the manual to first simple introduction of ideas:

First, we have a few basic guidelines:

1: If a zval refcount increase, then this zval is still in use, not a garbage

2: If a zval refcount is reduced to 0, then Zval can be released, not garbage

3: If a zval refcount is reduced by more than 0, then this zval can not be released, this zval may become a garbage

Only under guideline 3 will the GC collect Zval and then use the new algorithm to determine if the zval is garbage. So how do you judge if such a variable is a real garbage?

In a nutshell, it is a refcount minus 1 operation for each element in this zval, and after the operation is completed, if Zval refcount=0, then this zval is a garbage.

The PHP5.3 is optimized for this major flaw. Although the basis is still reference counting, some improvements have been made to control the memory leaks caused by ring references to a certain scale. Of course, this is not to say you can abuse the memory casually, write code still need to be careful!
Additional Highlights:
The 1.PHP script finishes running and all the memory space requested by the script is freed, regardless of whether there is a ring reference. Therefore, the issue of ring-referenced memory leaks generally only affects long-running program scripts.

2. The garbage collection mechanism needs to meet certain conditions before it can be executed. Therefore, after unset, the system does not necessarily recycle rubbish immediately.

The role of 3.unset.
"Unset just disconnects a variable into a chunk of memory, and then counts the reference count of that memory area at 1". That is, if more than one variable points to the same memory area, or if there is a ring reference, then unset does not release the memory area. Disconnecting also indicates that unset does not directly delete the memory area, but only changes its reference count.

4. $a the role of =null.
"$a = null is to empty the data structure pointed to by $ A directly, and its reference count to 0". Based on my understanding of this definition, the =null operation can immediately free up memory space!

As a result, many PHP tips have been very tedious to say to us, first set the variable to null, and then unset. After understanding its deep-seated principles, I thoroughly understood the reason for doing so! =null is the root!

Understanding the garbage collection mechanism of PHP

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.