In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
The main content of this article is "A brief explanation of PHP garbage collection mechanism". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the "simple explanation of PHP garbage collection mechanism".
What is garbage collection?
When using C, we all use free manually to free memory, and most programming languages after C will have a processing power such as garbage collection, which is the garbage collection mechanism we are going to talk about today, also known as GC. In GC-capable development languages, we don't need to care about when memory is released, or even we don't need to know about it at all, because these languages have helped us deal with memory release at the bottom.
Of course, the most famous aspect of this content is the garbage collection mechanism in Java, in fact, PHP also has a corresponding processing mechanism, of course, many PHPer may have never been in contact with, today we will discuss this aspect.
Garbage collection algorithm of PHP
In the previous article, we introduced the concept of reference counting. Before PHP5.3, PHP's garbage collection mechanism was very simple, that is, all cleanups with a refcount of 0 were recycled and dropped at the bottom, that is, free. But this approach brings a problem, that is, the circular references that we talked about in this article on citation counting, which cannot be recycled by the normal way of judging refcount. So before PHP5.3, circular references can cause memory leaks.
The emphasis on the version is because after 5. 3, PHP improved the garbage collection algorithm so that this kind of circular reference was resolved. (of course, we try to avoid this problem of circular references in our daily development.) For the specific algorithm, we quote the official picture:
There is a detailed explanation in the official document, but it will still be confused. Let's describe the process in simple language (speaking human words).
First of all, we have the concept of a root buffer, which is the root in the figure. At the bottom, we can find a possible root of each variable through a series of algorithms that we don't understand. PHP puts the possible root of the variable into the root buffer.
When the root buffer is full, the default value is usually 10000. You need to modify the source code and recompile to change this value. PHP will start the garbage collection mechanism, search for all variables related to this possible root from the root buffer according to the depth traversal algorithm, and subtract 1 from the refcount of a variable found by a possible root to mark the current "subtracted".
Then deep traversal again, if the refcount is not 0, add 1, if it is 0, it remains the same.
Then clear all possible roots in the root buffer, clearing rather than deleting. Then clean up and release the contents of all variables with a refcount of 0.
Are you already confused? In fact, I am also very confused, do not know how to write this paragraph.
Remember a few key points to deal with the interview and kill most people in a second.
After PHP5.3, we do not directly see whether the refcount of each variable is zero. The algorithm used is depth traversal, there is a root buffer, and it is cleaned up according to it. The specific algorithm needs a relatively solid C and algorithm foundation. When you learn the source code, you will study it carefully. 5.3.After using the algorithm to solve the problem of circular reference, the memory leak value will remain in a certain range. There will be no immediate large-scale crash the impact of garbage collection on performance
As mentioned earlier, garbage collection is performed as soon as the root buffer is full. There will also be two deep traverses, which will inevitably lead to performance consumption. After all, the implementation of algorithms is time-consuming. However, compared with the devastating error of memory overflow, the performance loss caused by garbage collection is basically negligible.
At this point, I believe you have a deeper understanding of the "simple explanation of PHP garbage collection mechanism". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.