In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "introduction of garbage collection related functions in PHP". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "introduction to garbage collection related functions in PHP"!
Garbage collection related functions in PHP
We have previously learned the concept of reference counting and garbage collection mechanisms in PHP. These contents are very theoretical, but also very common interview content. Today, however, we introduce some specific functions about garbage collection. For the previous two introduction articles, you can check it at the bottom of the article.
Further discussion on circular references and forced cleaning of circular references
Why should we emphasize "circular reference"? In fact, by default, when we directly unset () off a variable that is not referenced by other variables, we will make the reference count of that variable 0. At this point, PHP's default garbage collection mechanism clears this variable directly. For example:
A = new stdClass;$b = new stdClass;$c = new stdClass;echo memory_get_usage (), PHP_EOL; / / 706528unset ($a); echo memory_get_usage (), PHP_EOL; / / 706488gc_collect_cycles (); echo memory_get_usage (), PHP_EOL; / / 706488
As you can see from the above code, after we unset () $a, the memory is directly reduced. However, if a circular reference is generated, then simply doing unset () will have no effect.
Class D {public $d;} $d = new D * *-> d = $d * echo memory_get_usage (), PHP_EOL; / / 706544unset ($d); echo memory_get_usage (), PHP_EOL; / / 706544gc_collect_cycles (); echo memory_get_usage (), PHP_EOL; / / 706488
In this code, we make a simple circular reference assignment to\ $d. There is no change in memory after using unset (). At this point, you can only use the gc_collect_cycles () function to force circular reference cleanup to get rid of invalid circular references in $d.
Yes, the point of this paragraph is the function gc_collect_cycles (). Under normal circumstances, it will not have any cleaning effect on ordinary variable references, of course, for ordinary variables, we can just unset () off. Its main function is to clean up circular references. As we learned before, there is a root buffer for circular reference counting, which generally holds 10000 possible roots to be cleaned by default. The purpose of gc_collect_cycles () is to clean up without waiting for the root buffer to be full (personal understanding). Please move on to the content of this garbage collection algorithm: some superficial understanding of the PHP garbage collection mechanism
In fact, in most cases, we don't need to pay much attention to the garbage collection of PHP, that is, we don't need to call the gc_collect_cycles () function manually. PHP-FPM will be released as a whole after each call, and all will be released after a simple execution of the CLI script. Yes, normally, PHP destroys all the content after a single execution, and the memory garbage naturally does not exist. However, when executing long daemon scripts, or when using the resident process framework (Swoole), you still need to pay attention to whether there are circular references. Because this program runs all the time, it is possible to cause a memory leak if there are a large number of circular reference objects.
Enable, close and view circular reference garbage collection status gc_disable (); echo gc_enabled (), PHP_EOL; / / gc_enable (); echo gc_enabled (), PHP_EOL; / / 1
There are three simple functions, gc_disable () to "disable the circular reference collector", gc_enable () to "turn on the circular reference collector", and gc_enabled () to see if the current circular reference collector is turned on.
Force the recycling of memory used by the Zend engine memory manager gc_mem_caches ()
There is no detailed description on the official website and the network, but by definition, its main function is to recover the memory used by the memory manager of the Zend engine at the bottom of PHP. It's good for everyone to understand this, and I've never used it before.
Get the information of the garbage collector $e = new stdClass;for ($I = 100 runs / 0) {$e-> list [] = $e;} unset ($e); gc_collect_cycles (); var_dump (gc_status ()); / / array (4) {/ / ["runs"] = > int (1) / / ["collected"] = > int (2) / ["threshold"] = > int (10001) / ["roots"] = > int (0) / /}
We still make a circular reference object, and then use gc_status () to see the status of circular references in the current garbage collector. As you can see from the returned content, runs has run 1, collected has collected 2, and the threshold threshold is 10001. The root may be gone (it has been recycled).
This function can check the performance of the code in the test environment to see if there are any abnormal circular references in our code, of course, the above explanation is only personal speculation, because there is really very little information about it. So I also hope that the god who has studied this aspect in depth can leave a message and give us some advice!
At this point, I believe you have a deeper understanding of the "introduction to garbage collection-related functions in PHP". 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.