Home Backend Development PHP Tutorial Garbage Collection A brief explanation of PHP garbage collection mechanism

Garbage Collection A brief explanation of PHP garbage collection mechanism

Jul 29, 2016 am 08:43 AM
Garbage collection

Although I am also a learner of PHP, I really didn’t know much about the garbage collection process inside PHP before. We just used unset, null, mysql_close, __destruct and other functions in our code to release objects to prevent memory overflow, so I found the following instructions under GG on the Internet and made a record: "PHP can automatically manage memory and clear objects that are no longer needed. PHP uses a simple garbage collection mechanism called reference counting. Every time Each object contains a reference counter, and each reference is connected to the object. When the reference leaves the living space or is set to NULL, the counter is decremented by 1. When the reference counter of an object is zero, PHP knows that you will. This object is no longer needed and the memory space it occupies is released.”
As we all know, the PHP engine itself is written in C. When mentioning C, we must mention GC (garbage collection). Through the PHP manual, we know that the PHP engine will automatically perform GC actions. So we can’t help but ask, what exactly does it do? How is it recycled? Is the & reference operation a pointer? When unset() is used to unset() a variable, is it really recycled? These seem to be issues mentioned in the manual, but if you analyze them carefully, you will find that they are far from simple and general. . Maybe someone will jump out and say: You can know it by looking at the PHP source code. Yes, after you read through the PHP source code, this problem will definitely go away. However, this article will only analyze these seemingly ordinary but commonly used problems from PHP itself. Of course, there are small details that have been overlooked. Of course, there are inevitably some omissions due to limited skill levels. We warmly welcome all phpers to discuss together.
First of all, let’s see an example, the simplest execution process:
Example 1: gc.php
error_reporting(E_ALL);
$a = 'I am test.';
$b = & $a;
echo $b ." ";
?>
Needless to say% php -f gc. The php output result is very clear:
hy0kl% php -f gc.php
I am test.
Okay, next:
Example 2:
error_reporting(E_ALL);
$a = 'I am test. ';
$b = & $a;
$b = 'I will change?';
echo $a ." ";
echo $b ." ";
?>
The execution result is still obvious:
hy0kl % php -f gc.php
I will change?
I will change?
Please see:
Example 3:
error_reporting(E_ALL);
$a = 'I am test.';
$ b = & $a;
unset($a);
echo $a ." ";
echo $b ." ";
?>
Do you have to think about it?
hy0kl% php -f gc. php
Notice: Undefined variable: a in /usr/local/www/apache22/data/test/gc.php on line 8
I am test.
Are you a little confused?
Look again:
Example 4:
error_reporting(E_ALL);
$a = 'I am test.';
$b = & $a;
unset($b);
echo $a ." ";
echo $b ." ";
?>
In fact, if you understand Example 3, this is similar to it.
hy0kl% php -f gc.php
I am test.
Notice: Undefined variable: b in /usr/local/www/apache22/ data/test/gc.php on line 9
Look here:
Example 5:
error_reporting(E_ALL);
$a = 'I am test.';
$b = & $a;
$a = null;
echo '$a = '. $a ." ";
echo '$b = '. $b ." ";
?>
What is the first feeling of fierceness?
hy0kl % php -f gc.php
$a =
$b =
Yes, this is the output result. PHPers who have a deep understanding of PHP GC will not find it strange. To be honest, when I ran this for the first time I was surprised when I saw this piece of code, but it gave me a deeper understanding of PHP GC. Then the following example of working with it is naturally easier to understand.
Example 6:
error_reporting(E_ALL);
$a = 'I am test.';
$b = & $a;
$b = null;
echo '$a = '. $a ." ";
echo '$b = '. $b ." ";
?>
OK, if the result of the above example does not have any details for the reader, then you can close this window and welcome to come back when you have time!
Let’s analyze GC and references in detail.
1. In all examples, a variable is created. To put it simply, this process opens up a space in the memory and stores a string I am test in it. There is a symbol table inside PHP to record references to each block of memory. Count, then the reference count of this memory will be increased by 1, and a label (variable) named $a will be used to point to this memory, so that the memory can be operated according to the label name.
2. Perform & on the variable $a Operation, my understanding is to find the memory pointed to by $a, establish the same reference point for $b, and add 1 to the reference count of the memory block storing the string I am test. in the symbol table. In other words, our When the script is executed to this line, the memory storing the string I am test. is referenced twice. What should be emphasized here is that the & operation establishes a reference pointer, not a pointer. PHP has no concept of pointers! At the same time, Some people have suggested that file soft links are similar to UNIX. It can be understood to a certain extent: the memory storing the character I am test. is a real file of ours, and the variables $a and $b are established for the real file. Soft links, but they point to the same real file. So, we see that when $b is assigned a value in Example 2, the value of $a also changes. It is similar to operating a file through a soft link.
3. In Examples 3 and 4, the unset() operation was performed. According to the actual execution results, it can be seen that: unset() only disconnects the reference of the variable to the memory it originally pointed to, making the variable itself undefined. If a null reference is passed, a Notice is issued when the call is made, and the reference count of that piece of memory in the symbol table is decremented by 1, and it does not affect other variables pointing to this piece of memory. In other words, only when a piece of memory has a reference count in the symbol table When it is 0, the PHP engine will reclaim this memory.
PHP Manual
4.0.0 unset() became an expression. (In PHP 3, unset() would always return 1).
What does this mean?
See Look at the following code and its results:
error_reporting(E_ALL);
$a = 'I am test.';
$b = & $a;
unset($a);
unset($a) ;
unset($a);
echo '$a = '. $a ." ";
echo '$b = '. $b ." ";
?>
hy0kl% php -f gc.php
Notice: Undefined variable: a in /usr/local/www/apache22/data/test/gc.php on line 10
$a =
$b = I am test.
The first unset() operation has been disconnected point, so subsequent operations will not affect the reference count of any memory in the symbol table.
4. Through Example 5 & 6, it can be clearly concluded that the assignment null operation is quite violent, it will directly change the variable The reference count of the memory pointed to in the symbol is set to 0, so this memory is naturally reclaimed by the engine. It is unknown when it will be used again. It may be used to store other information immediately, or it may never be used again. Passed. But no matter what, all the original variables pointing to that memory will no longer be able to operate the reclaimed memory, and any variable trying to call it will return null.
error_reporting(E_ALL);
$a = 'I am test.';
$b = & $a;
$b = null;
echo '$a = '. $a ." ";
echo '$b = '. $b ." ";
if (null === $a)
{
echo '$a is null.';
} else
{
echo 'The type of $a is unknown.';
}
?>
hy0kl% php - f gc.php
$a =
$b =
$a is null.
To sum up, it fully explains why when we look at the source code of open source products, we often see some relatively large temporary variables, or after use Reused information that is no longer called will be concentrated or displayed as null. It is equivalent to directly killing the real file in UNIX, and all soft links pointing to it will naturally become empty links.

The above introduces the garbage collection. A brief explanation of the PHP garbage collection mechanism, including the content of garbage collection. I hope it will be helpful to friends who are interested in PHP tutorials.

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)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months 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)

How to avoid memory leaks in C# development How to avoid memory leaks in C# development Oct 08, 2023 am 09:36 AM

How to avoid memory leaks in C# development requires specific code examples. Memory leaks are one of the common problems in the software development process, especially when developing using the C# language. Memory leaks cause applications to take up more and more memory space, eventually causing the program to run slowly or even crash. In order to avoid memory leaks, we need to pay attention to some common problems and take corresponding measures. Timely release of resources In C#, resources must be released in time after use, especially when it involves file operations, database connections, network requests and other resources. Can

Common memory management problems and solutions in C# Common memory management problems and solutions in C# Oct 11, 2023 am 09:21 AM

Common memory management problems and solutions in C#, specific code examples are required. In C# development, memory management is an important issue. Incorrect memory management may lead to memory leaks and performance problems. This article will introduce readers to common memory management problems in C#, provide solutions, and give specific code examples. I hope it can help readers better understand and master memory management technology. The garbage collector does not release resources in time. The garbage collector (GarbageCollector) in C# is responsible for automatically releasing resources and no longer using them.

What is the relationship between memory management techniques and security in Java functions? What is the relationship between memory management techniques and security in Java functions? May 02, 2024 pm 01:06 PM

Memory management in Java involves automatic memory management, using garbage collection and reference counting to allocate, use and reclaim memory. Effective memory management is crucial for security because it prevents buffer overflows, wild pointers, and memory leaks, thereby improving the safety of your program. For example, by properly releasing objects that are no longer needed, you can avoid memory leaks, thereby improving program performance and preventing crashes.

Memory management and garbage collection technology in PHP Memory management and garbage collection technology in PHP May 11, 2023 am 08:33 AM

As a widely used scripting language, PHP has unique memory management and garbage collection technology to ensure efficient execution at runtime. This article will briefly introduce the principles and implementation methods of PHP memory management and garbage collection. 1. Principle of PHP memory management PHP's memory management is implemented by reference counting (ReferenceCounting). This method is one of the more common memory management methods in modern languages. When a variable is used, PHP will allocate a memory for it and store this memory

Memory management problems and solutions encountered in Python development Memory management problems and solutions encountered in Python development Oct 09, 2023 pm 09:36 PM

Summary of memory management problems and solutions encountered in Python development: In the Python development process, memory management is an important issue. This article will discuss some common memory management problems and introduce corresponding solutions, including reference counting, garbage collection mechanism, memory allocation, memory leaks, etc. Specific code examples are provided to help readers better understand and deal with these issues. Reference Counting Python uses reference counting to manage memory. Reference counting is a simple and efficient memory management method that records every

How to use Go language for memory optimization and garbage collection How to use Go language for memory optimization and garbage collection Sep 29, 2023 pm 05:37 PM

How to use Go language for memory optimization and garbage collection. As a high-performance, concurrent, and efficient programming language, Go language has good support for memory optimization and garbage collection. When developing Go programs, properly managing and optimizing memory usage can improve the performance and reliability of the program. Use appropriate data structures In the Go language, choosing the appropriate data structure has a great impact on memory usage. For example, for collections that require frequent additions and deletions of elements, using linked lists instead of arrays can reduce memory fragmentation. in addition,

Analysis of Python's underlying technology: how to implement garbage collection mechanism Analysis of Python's underlying technology: how to implement garbage collection mechanism Nov 08, 2023 pm 07:28 PM

Analysis of Python's underlying technology: How to implement the garbage collection mechanism requires specific code examples Introduction: Python, as a high-level programming language, is extremely convenient and flexible in development, but its underlying implementation is quite complex. This article will focus on exploring Python's garbage collection mechanism, including the principles, algorithms, and specific implementation code examples of garbage collection. I hope that through this article’s analysis of Python’s garbage collection mechanism, readers can have a deeper understanding of Python’s underlying technology. 1. Principle of garbage collection First of all, I

How to solve the problem of insufficient heap memory space in Java development How to solve the problem of insufficient heap memory space in Java development Jun 29, 2023 am 11:11 AM

Java is a widely used programming language. Due to its automatic memory management mechanism, especially the garbage collection mechanism, developers do not need to pay too much attention to the allocation and release of memory. However, in some special cases, such as when processing large data or running complex algorithms, Java programs may encounter problems with insufficient heap memory space. This article discusses how to solve this problem. 1. Understand the heap memory space Heap memory is the memory space allocated in the Java Virtual Machine (JVM) for use by Java programs when running. it stores

See all articles