Table of Contents
Detailed explanation of PHP’s garbage collection mechanism
Home Backend Development PHP Tutorial Detailed explanation of PHP's garbage collection mechanism_PHP tutorial

Detailed explanation of PHP's garbage collection mechanism_PHP tutorial

Jul 13, 2016 am 10:10 AM
Rubbish mechanism

Detailed explanation of PHP’s garbage collection mechanism

Recently, I wrote a script using PHP to simulate the implementation of a daemon process, so I need to have an in-depth understanding of the garbage collection mechanism in PHP. This article refers to the PHP manual.
Before understanding the PHP garbage collection mechanism (GC), first understand the storage of variables.
Variables in PHP exist in a zval variable container. The structure is as follows:
Type
Value
is_ref
refcount
In zval, in addition to storing the type and value of the variable, there are also is_ref fields and refcount fields.
is_ref: It is a bool value, used to distinguish whether the variable belongs to the reference set. What does it mean? You can think of it this way: indicating whether the variable has more than one alias.
refcount: Counter, indicating the number of variables pointing to this zval variable container.
There is a default relationship between the two: when the refcount value is 1, the value of is_ref is false. Because refcount is 1, this variable cannot have multiple aliases, so there is no reference.
After installing the xdebug extension, you can use xdebug_debug_zval to print out the zval container details.
One thing to note here is that when assigning a variable = to another variable, memory space will not be allocated immediately for the new variable, but 1 will be added to the refcount in the zval of the original variable. Only when the original variable changes, memory space will be allocated for the new variable, and the refcount of the original variable will be reduced by 1. Of course, if you unset the original variable, the new variable directly uses the zval of the original variable instead of reallocating it.
When & is assigned by reference, the is_ref of the original variable becomes 1, and the refcount is increased by 1. If a variable & is assigned a value, the variable previously assigned with = will allocate space.
$a = 1;
xdebug_debug_zval('a');
echo PHP_EOL;
$b = $a;
xdebug_debug_zval('a');
echo PHP_EOL;
$c = &$a;
xdebug_debug_zval('a');
echo PHP_EOL;
xdebug_debug_zval('b');
echo PHP_EOL;
?>
The running results are as follows:
a:(refcount=1, is_ref=0),int 1
a:(refcount=2, is_ref=0),int 1
a:(refcount=2, is_ref=1),int 1
b:(refcount=1, is_ref=0),int 1
The zval described above stores scalars, so how are composite type arrays stored?
$a = array( 'meaning' => 'life', 'number' => 42 );
xdebug_debug_zval( 'a' );
echo PHP_EOL;
class Test{
public $a = 1;
public $b = 2;
function handle(){
echo 'hehe';
}
}
$test = new Test();
xdebug_debug_zval('test');
?>
The running results are as follows:
a:(refcount=1, is_ref=0),
array
'meaning' => (refcount=1, is_ref=0),
string
'life' (length=4)
'number' => (refcount=1, is_ref=0),
int
42
test:(refcount=1, is_ref=0),
object(Test)[1]
public 'a' => (refcount=2, is_ref=0),
int
1
public 'b' => (refcount=2, is_ref=0),
int
2
It can be seen that the array uses 1 more zval storage than the array length. Objects are similar. The storage representation of the array is given below
You can see: the array is allocated three zval containers: a meaning number
Now let’s see how the so-called circular reference is generated
$a = array( 'one' );
$a[] =& $a;
xdebug_debug_zval( 'a' );
?>
Running results:
a:(refcount=2, is_ref=1),
array
0 => (refcount=1, is_ref=0),
string
'one' (length=3)
1 => (refcount=2, is_ref=1), &array
The zval container of a and 1 are the same. As follows:
This forms a circular reference.
In PHP 5.2 and earlier versions, there is no dedicated garbage collector GC (Garbage Collection). When the engine determines whether a variable space can be released, it relies on the value of the refcount of the variable's zval. If refcount If it is 0, then the variable space can be released, otherwise it will not be released. This is a very simple GC implementation.
Now unset ($a), then the refcount of the array is reduced by 1 and becomes 1. Now there is no variable pointing to this zval, and the counter of this zval is 1 and will not be recycled.
Although there is no longer any symbol in a scope pointing to this structure (that is, the variable container), since the array element "1" still points to the array itself, this container cannot be cleared. Since there is no other symbol pointing to it, the user has no way to clear the structure, resulting in a memory leak. Fortunately, PHP will clear this data structure at the end of the request, but before PHP clears it, it will consume a lot of space in memory. This happens a lot if you're implementing a parsing algorithm, or doing other things like having a child element point to its parent. Of course, the same situation can happen with objects, in fact it is more likely to happen with objects, because objects are always implicitly referenced.
It’s okay if the above situation occurs only once or twice, but if memory leaks occur thousands or even hundreds of thousands of times, this is obviously a big problem. Problems can arise when running long-running scripts, such as daemons that rarely end when requesting them, and memory space will continue to be consumed, leading to out-of-memory crashes.
In PHP5.3, a special algorithm (more complex) is used. , to deal with the problem of memory leaks caused by circular references.
When a zval may be garbage, the collection algorithm will put the zval into a memory buffer. When the buffer reaches the maximum critical value (the maximum value can be set), the recycling algorithm will cycle through all zvals in the buffer to determine whether they are garbage and perform release processing. Or we use gc_collect_cycles in the script to force the garbage in the buffer to be recycled.
In the GC of php5.3, the garbage is explained as follows:
1: If the refcount of a zval increases, then this zval is still in use, it is definitely not garbage and will not enter the buffer
2: If the refcount of a zval is reduced to 0, then the zval will be released immediately. It is not a garbage object to be processed by the GC and will not enter the buffer.
3: If the refcount of a zval is greater than 0 after being reduced, then the zval cannot be released, and the zval may become garbage and be put into the buffer. The GC in PHP5.3 targets this kind of zval processing.
Turning on/off the garbage collection mechanism can be achieved by modifying the PHP configuration, or it can be turned on and off in the program using gc_enable() and gc_disable().
After turning on the garbage collection mechanism, a large amount of memory space can be saved in case of memory leaks. However, since the garbage collection algorithm takes time to run, turning on the garbage collection algorithm will increase the execution time of the script.
The following is a script given in the php manual
class Foo
{
public $var = '3.1415962654';
}
$baseMemory = memory_get_usage();
for ( $i = 0; $i <= 100000; $i++ )
{
$a = new Foo;
$a->self = $a;
if ( $i % 500 === 0 )
{
echo sprintf( '%8d: ', $i ), memory_get_usage() - $baseMemory, "n";
}
}
?>
 
For this script, its memory usage in php5.2 and 5.3 is given, as shown below:
For the following script
class Foo
{
public $var = '3.1415962654';
}
for ( $i = 0; $i <= 1000000; $i++ )
{
$a = new Foo;
$a->self = $a;
}
echo memory_get_peak_usage(), "n";
?>
 
When the garbage collection mechanism is turned on, the script execution time increases by 7% compared to when it is not turned on
Usually, the garbage collection mechanism in PHP only increases the time consumption when the recycling algorithm is actually running. But in normal (smaller) scripts there should be no performance impact at all.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/938943.htmlTechArticleDetailed explanation of PHP’s garbage collection mechanism. Recently, I wrote a script using php to simulate the implementation of a daemon process, so I need In-depth understanding of the garbage collection mechanism in PHP. This article refers to...
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

An in-depth exploration of the storage location and mechanism of Golang variables An in-depth exploration of the storage location and mechanism of Golang variables Feb 28, 2024 pm 09:45 PM

Title: An in-depth exploration of the storage location and mechanism of Golang variables. As the application of Go language (Golang) gradually increases in the fields of cloud computing, big data and artificial intelligence, it is particularly important to have an in-depth understanding of the storage location and mechanism of Golang variables. In this article, we will discuss in detail the memory allocation, storage location and related mechanisms of variables in Golang. Through specific code examples, it helps readers better understand how Golang variables are stored and managed in memory. 1.Memory of Golang variables

Deep understanding of the mechanics of CSS layout recalculation and rendering Deep understanding of the mechanics of CSS layout recalculation and rendering Jan 26, 2024 am 09:11 AM

CSS reflow and repaint are very important concepts in web page performance optimization. When developing web pages, understanding how these two concepts work can help us improve the response speed and user experience of the web page. This article will delve into the mechanics of CSS reflow and repaint, and provide specific code examples. 1. What is CSS reflow? When the visibility, size or position of elements in the DOM structure changes, the browser needs to recalculate and apply CSS styles and then re-layout

Step-by-step tutorial on how to clean up system junk in Windows 10 Step-by-step tutorial on how to clean up system junk in Windows 10 Jul 13, 2023 pm 09:53 PM

When some users use computers, Win10 system cleans up junk. When encountering Win10 system cleaning up junk, how should we solve it? Nowadays, there are still many users who don’t know how to deal with the situation of cleaning up garbage in Win10 system, so let’s take a look at the step-by-step tutorial on cleaning up system junk in Win10. Let’s take a look. 1. On the computer desktop, click "This PC" and manage. 2. Select Disk Management 3. Select the disk you want to clean, right-click and select Properties. 4. Click the "Disk Cleanup" button at the bottom of the page. 5. In the interface that appears, select the files you want to clean, click "OK", and click "Delete Files". The above is a step-by-step tutorial on how to clean up system junk in Windows 10. I hope

Autoloading mechanism in PHP Autoloading mechanism in PHP Jun 18, 2023 pm 01:11 PM

As the PHP language becomes more and more popular, developers need to use more and more classes and functions. When a project grows in size, manually introducing all dependencies becomes impractical. At this time, an automatic loading mechanism is needed to simplify the code development and maintenance process. The auto-loading mechanism is a feature of the PHP language that can automatically load required classes and interfaces at runtime and reduce manual class file introduction. In this way, programmers can focus on developing code and reduce errors and time waste caused by tedious manual class introduction. In PHP, generally

Analysis of implicit conversion mechanism in PHP Analysis of implicit conversion mechanism in PHP Mar 09, 2024 am 08:00 AM

Analysis of the implicit conversion mechanism in PHP In PHP programming, implicit conversion refers to the process by which PHP automatically converts one data type to another data type without explicitly specifying type conversion. The implicit conversion mechanism is very common in programming, but it can also easily cause some unexpected bugs. Therefore, understanding the principles and rules of the implicit conversion mechanism is very important for writing robust PHP code. 1. Implicit conversion between integer and floating point types In PHP, implicit conversion between integer and floating point types is very common. When an integer

How to use error handling mechanism in Go? How to use error handling mechanism in Go? May 11, 2023 pm 04:45 PM

As a strongly typed, efficient and modern programming language, Go language has been increasingly widely used in modern software development. Among them, the error handling mechanism is an aspect of the Go language that deserves attention, and the error handling mechanism of the Go language is also very different and superior compared to other programming languages. This article will introduce the basic concepts, implementation methods and best practices of the error handling mechanism in Go language to help readers better understand and use the error handling mechanism in Go language. 1. The basic concepts of Go language error handling mechanism in Go

What is the memory management mechanism in Go language? What is the memory management mechanism in Go language? Jun 10, 2023 pm 04:04 PM

Go language is an efficient programming language widely used for system-level programming. One of its main advantages is its memory management mechanism. The built-in garbage collection mechanism (GarbageCollection, referred to as GC) in the Go language eliminates the need for programmers to perform memory allocation and release operations themselves, improving development efficiency and code quality. This article will provide a detailed introduction to the memory management mechanism in the Go language. 1. Go memory allocation In the Go language, memory allocation uses two heap areas: small object heap (small

Reveal the mechanism of HTTP status code exceptions Reveal the mechanism of HTTP status code exceptions Feb 19, 2024 pm 08:26 PM

Revealing the mechanism of HTTP status code exceptions. HTTP status code refers to a numeric code returned by the server to the client when communicating between the client and the server. It is used to indicate the processing status of the request. The HTTP protocol defines a series of status codes, each status code has a specific meaning. Under normal circumstances, the server will return the corresponding status code based on the processing result of the request, thereby informing the client of the current processing status. However, sometimes we encounter HTTP status code exceptions, that is, the server returns unexpected status

See all articles