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 > Development >
Share
Shulou(Shulou.com)06/03 Report--
Xiaobian to share with you how PHP garbage collection mechanism to deal with memory leaks, I believe most people still do not know how to share this article for your reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!
The code is as follows:
$a = array(1, 2, &$a);
unset($a);
In older PHP versions, memory leaks occur here, analyzed as follows:
Executing the first line, you can know that $a and $a[2] point to zval refcount=2, is_ref=1
Then execute the second line,$a will be removed from the symbol table and point to refcount--of zval, where refcount=1, because refcount!= 0, so zval won't be garbage collected, but we've lost the entry $a[2] pointing to this zval, so this zval becomes a memory garbage
The same thing can happen to references inside classes, for example
The copy code is as follows:
$a = new Man();
$a->self = &$a;
unset($a);
So how to solve this problem, the new GC mechanism uses an algorithm to solve this problem
PHP has a root buffer to store zval node information, GC algorithm starts when root buffer is full or gc function is manually called
For an array or class type zval, when the garbage collection mechanism is started, the algorithm will traverse the zval of the elements/members within the array/class of the zval once and subtract 1 from the refcount. If the refcount of the zval is reduced to 0 after the traversal is completed, it means that the zval is a memory garbage and will be destroyed. See the following example.
The copy code is as follows:
$a = array(1, 2, &$a, &$a);
unset($a);
It is easy to know zval to which $a points, assuming refcount= 3 for z1, is_ref =1
When unset($a) executes,$a is removed from the symbol table and we lose access to z1, where z1 refcount=2, is_ref=1
When GC is started, it will traverse the refcount of zval of the array element of z1 minus 1. When traversing to a[2],z1 refcount--, z1 refcount--when a[3], z1 refcount = 0, z1 can be marked as memory garbage, and the algorithm will collect it later.
In summary, it can be stated as follows: If an array type zval, its element zval is iterated once, and the refcount--of the iterated zval is also iterated. If the final refcount=0 zval, it is garbage and needs to be collected.
The above is "PHP garbage collection mechanism how to handle memory leaks" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!
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.