


A brief discussion on the evolution of garbage collection algorithm in PHP 5_PHP Tutorial
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:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.
