Home  >  Article  >  Backend Development  >  A brief discussion on the evolution of garbage collection algorithm in PHP 5_PHP Tutorial

A brief discussion on the evolution of garbage collection algorithm in PHP 5_PHP Tutorial

WBOY
WBOYOriginal
2016-07-22 09:02:29693browse

PHP is a managed language. In PHP programming, programmers do not need to manually handle the allocation and release of memory resources (except when using C to write PHP or Zend extensions), which means that PHP itself implements a garbage collection mechanism (Garbage Collection). Now if you go to the official PHP website (php.net) you can see that the two current branch versions of PHP5, PHP5.2 and PHP5.3, are updated separately. This is because many projects still use the 5.2 version of PHP, and the 5.3 version is 5.2 is not fully compatible. PHP5.3 has made many improvements based on PHP5.2, among which the garbage collection algorithm is a relatively big change. This article will discuss the garbage collection mechanisms of PHP5.2 and PHP5.3 respectively, and discuss the impact of this evolution and improvement on programmers writing PHP and the issues they should pay attention to.

The internal representation of PHP variables and associated memory objects

In the final analysis, garbage collection is the operation of variables and their associated memory objects, so before discussing PHP’s garbage collection mechanism, let’s briefly introduce the variables and their memory in PHP The object's internal representation (its representation in its C source code).

The official PHP documentation divides variables in PHP into two categories: scalar types and complex types. Scalar types include booleans, integers, floating point types and strings; complex types include arrays, objects and resources; there is also a special NULL, which is not divided into any type, but becomes a separate category.

All these types are uniformly represented by a structure called zval within PHP. In the PHP source code, the name of this structure is "_zval_struct". The specific definition of zval is in the "Zend/zend.h" file of the PHP source code. The following is an excerpt of the relevant code.

<ol class="dp-c">
<li class="alt"><span><span>typedef union _zvalue_value {  </span></span></li>
<li>
<span>    long lval;                  </span><span class="comment">/* long value */</span><span> </span>
</li>
<li class="alt">
<span>    double dval;                </span><span class="comment">/* double value */</span><span> </span>
</li>
<li><span>    struct {  </span></li>
<li class="alt"><span>        char *val;  </span></li>
<li><span>        int len;  </span></li>
<li class="alt"><span>    } str;  </span></li>
<li>
<span>    HashTable *ht;              </span><span class="comment">/* hash table value */</span><span> </span>
</li>
<li class="alt"><span>    zend_object_value obj;  </span></li>
<li><span>} zvalue_value;  </span></li>
<li class="alt"><span> </span></li>
<li><span>struct _zval_struct {  </span></li>
<li class="alt">
<span>    </span><span class="comment">/* Variable information */</span><span> </span>
</li>
<li><span>    zvalue_value value;       </span></li>
<li class="alt">
<span class="comment">/* value */</span><span> </span>
</li>
<li><span>    zend_uint refcount__gc;  </span></li>
<li class="alt">
<span>    zend_uchar type;    </span><span class="comment">/* active type */</span><span> </span>
</li>
<li><span>    zend_uchar is_ref__gc;  </span></li>
<li class="alt"><span>}; </span></li>
</ol>

The union "_zvalue_value" is used to represent the values ​​of all variables in PHP. The reason why union is used here is because a zval can only represent one type of variable at a time. You can see that there are only 5 fields in _zvalue_value, but there are 8 data types in PHP including NULL. So how does PHP use 5 fields to represent 8 types internally? This is a clever place in PHP design. It uses Reusing fields achieves the purpose of reducing fields. For example, within PHP, Boolean types, integers and resources (as long as the identifier of the resource is stored) are stored through the lval field; dval is used to store floating point types; str stores strings; ht stores arrays (note that in PHP The array is actually a hash table); and obj stores the object type; if all fields are set to 0 or NULL, it means NULL in PHP, so that 5 fields are used to store 8 types of values.

The type of value in the current zval (the type of value is _zvalue_value) is determined by the type in "_zval_struct". _zval_struct is the specific implementation of zval in C language. Each zval represents a memory object of a variable. In addition to value and type, you can see that there are two fields refcount__gc and is_ref__gc in _zval_struct. From their suffixes, you can conclude that these two guys are related to garbage collection. That's right, PHP's garbage collection relies entirely on these two fields. Among them, refcount__gc indicates that there are several variables currently referencing this zval, and is_ref__gc indicates whether the current zval is referenced by reference. This sounds very confusing. This is related to the "Write-On-Copy" mechanism of zval in PHP. Since this topic is not This article is the focus, so I won’t go into details here. Readers only need to remember the role of the refcount__gc field.

Garbage collection algorithm in PHP5.2 - Reference Counting

The memory recycling algorithm used in PHP5.2 is the famous Reference Counting. The Chinese translation of this algorithm is called "reference counting". Its idea is very intuitive and concise: A counter is assigned to each memory object. When a memory object is created, the counter is initialized to 1 (so there is always a variable referencing this object). Every time a new variable refers to this memory object, the counter is incremented by 1, and Whenever a variable that refers to this memory object is reduced, the counter is decremented by 1. When the garbage collection mechanism operates, all memory objects with a counter of 0 are destroyed and the memory they occupy is recycled. The memory object in PHP is zval, and the counter is refcount__gc.

For example, the following PHP code demonstrates the working principle of the PHP5.2 counter (the counter value is obtained through xdebug):

<ol class="dp-c"><li class="alt"><span><span><?php  </span></span></li><li><span> </span></li><li class="alt"><span class="vars">$val1</span><span> = 100; </span><span class="comment">//zval(val1).refcount_gc = 1; </span><span> </span></li><li><span class="vars">$val2</span><span> = </span><span class="vars">$val1</span><span>; </span><span class="comment">//zval(val1).refcount_gc = 2,zval(val2).refcount_gc = 2(因为是Write on copy,当前val2与val1共同引用一个zval) </span><span> </span></li><li class="alt"><span class="vars">$val2</span><span> = 200; </span><span class="comment">//zval(val1).refcount_gc = 1,zval(val2).refcount_gc = 1(此处val2新建了一个zval) </span><span> </span></li><li><span>unset(</span><span class="vars">$val1</span><span>); </span><span class="comment">//zval(val1).refcount_gc = 0($val1引用的zval再也不可用,会被GC回收) </span><span> </span></li><li class="alt"><span> </span></li><li><span>?> </span></span></li></ol>

Reference Counting is simple and intuitive, and easy to implement, but it has a fatal flaw, which is that it easily causes memory leaks. Many friends may have realized that if there is a circular reference, Reference Counting may cause memory leaks. For example, the following code:

<ol class="dp-c"><li class="alt"><span><span><?php  </span></span></li><li><span> </span></li><li class="alt"><span class="vars">$a</span><span> = </span><span class="keyword">array</span><span>();  </span></li><li><span class="vars">$a</span><span>[] = & </span><span class="vars">$a</span><span>;  </span></li><li class="alt"><span>unset(</span><span class="vars">$a</span><span>);  </span></li><li><span> </span></li><li class="alt"><span>?> </span></span></li></ol>

This code first creates the array a, and then lets the first element of a point to a by reference. At this time, the refcount of zval of a becomes 2, and then we destroy the variable a. At this time The refcount of the zval initially pointed to by a is 1, but we can no longer operate on it because it forms a circular self-reference, as shown in the figure below:


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445838.htmlTechArticle PHP is a managed language. In PHP programming, programmers do not need to manually handle the allocation and release of memory resources (except when using C to write PHP or Zend extensions), which means that PHP itself implements garbage...
Statement:
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