Home  >  Article  >  Backend Development  >  What is garbage collection in PHP? What impact does it have on performance?

What is garbage collection in PHP? What impact does it have on performance?

青灯夜游
青灯夜游forward
2021-07-29 18:28:072257browse

This article will introduce you to garbage collection, briefly analyze the garbage collection mechanism in PHP, and finally talk about the impact of garbage collection on performance. Let’s take a look!

What is garbage collection in PHP? What impact does it have on performance?

# I believe that students who have learned a little bit about development know that regardless of any programming language, a variable will be stored in memory. In fact, we developers are constantly manipulating memory back and forth. Correspondingly, if we keep adding new variables, the memory will keep increasing. If there is no good mechanism, then the memory will increase without limit and eventually fill up. All memory. This causes a memory leak. But in daily development, unless a large file is loaded at a time, we almost never see memory overrun errors. This is the role of the garbage collection mechanism.

What is garbage collection?

When using C language, we have to manually use free to release memory. Most programming languages ​​after C will have their own processing capabilities such as garbage collection, which is what we have today The garbage collection mechanism we are talking about is also called GC. In development languages ​​​​with GC capabilities, we do not need to care about when to release memory, and we do not even need to understand the content of this piece at all, because these languages ​​​​have already handled the issue of memory release for us at the bottom level.

Of course, the most famous content in this area is the garbage collection mechanism in Java. In fact, PHP also has a corresponding processing mechanism. Of course, many PHPers may have never been exposed to it. Today we will discuss this aspect. .

PHP’s garbage collection algorithm

In the previous article, we introduced the concept of reference counting. Before PHP5.3, PHP's garbage collection mechanism was very simple, which was to clean up and recycle all items with a refcount of 0, which was free at the bottom level. But this method will bring about a problem, which is the circular reference we talked about in the article on reference counting. This kind of reference problem cannot be recycled through ordinary judgment of refcount. So before PHP5.3, circular references would cause memory leaks.

The reason why I emphasize the version is because after 5.3, PHP improved the garbage collection algorithm and solved this circular reference. (Of course, we try to avoid this kind of circular reference problem in daily development). For the specific algorithm, we quote the official picture:

What is garbage collection in PHP? What impact does it have on performance?

There is a detailed explanation in the official document, but it is still very confusing. Let’s use simple language (human language) to describe this process.

First of all, we have the concept of root buffer, which is the root in the picture. At the bottom level, through a series of incomprehensible algorithms, we can find a possible root of each variable. PHP will put the possible roots of the variable into the root buffer.

When the root buffer is full, the default value is generally 10000. You need to modify the source code and recompile to modify this value. PHP will start the garbage collection mechanism, follow the depth traversal algorithm from the root buffer to find all the variables related to this possible root, and reduce the refcount of the variable found by a certain possible root by 1, and make a mark to mark the current one. "Reduced".

Then traverse deeply again. If refcount is not 0, add 1. If it is 0, keep it unchanged.

Then clear all possible roots in the root buffer, clearing rather than deleting. Then clean and release all variable contents with refcount of 0.

Are you confused already? In fact, I am also very confused. I don’t even know how this paragraph was written....

Just remember a few key points and you can handle the interview and kill most people instantly.

  • After PHP5.3, we do not directly check whether the refcount of each variable is 0.
  • The algorithm used is depth traversal. There is a root buffer and it is cleaned according to it. The specific algorithm requires a solid C and algorithm foundation, so study it carefully when learning the source code
  • 5.3 Later, the algorithm solved the problem of circular references
  • The memory leak value will remain within a certain range , there will be no immediate large-scale crash

The impact of garbage collection on performance

As mentioned earlier, garbage collection will occur after the root buffer is full It will be executed immediately. There will also be two depth traversals, which inevitably leads to performance consumption. After all, the execution of algorithms takes time. However, compared with devastating errors such as memory overflow, the performance loss caused by garbage collection is basically negligible.

Summary

In fact, we only need to remember a few key points about garbage collection. The specific core algorithm and content need to be studied in more depth. You can only fully understand it after reading the source code. Of course, this is also the goal of our study. We will definitely get involved in the underlying content of the source code in the future, so let us wait and see!

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What is garbage collection in PHP? What impact does it have on performance?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete