Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Principle Analysis of PHP garbage Collection Mechanism

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the principle of PHP garbage collection mechanism analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

PHP garbage collection mechanism

1. Each variable is defined in a container called zval, which contains the number of types and values, as well as two additional information: refcount (understood as the number of variables) and is_ref (understood as whether it is a reference variable). When the variable is referenced once, refcount will be + 1, and when you unset it, the value will be reduced by 1 until it is 0 and will be deleted from memory.

2. When defining a variable, it does not always expand the predetermined value, because PHP will pre-occupy a space in memory and will allocate it to you when you declare the variable, but when you exceed this preoccupied space, it will increase the space, but when you delete the variable, the space capacity will not disappear immediately.

3. The reference to the variable will not increase the memory footprint alone, it will point to the zval structure, just refcount+1

4. To put it simply, the variable of PHP depends on an internal implementation of symbol_table symbol table, and the basic implementation of symbol table is HashTable, which is consistent with the basic implementation of PHP array. Because of the existence of the symbol table, we can use global to mark global variables and use functions such as compact to pull variables directly from the current symbol table.

When talking about whether the unset ($a) mentioned by the subject will release the space immediately, the answer is no. Unset supports deleting the element named a from the symbol table (just marking that the space is available again, not freeing it).

In addition, in the case of repeatedly updating $key in the loop, because the updated variables are variables with the same name, they are the same element in the symbol table, and the same position is updated when the update is made, and the value of the previous element is overwritten immediately.

When it comes to declaring that the memory of new variables will increase, the answer is uncertain. This is because the symbol table is based on the characteristics of HashTable implementation. HashTable does not apply for memory for one element for each additional element, but applies for memory for multiple elements at a time (only these location marks are unused), and when the HashTable is full, request memory for new multiple elements. That is, when we declare or assign a variable, if it is not in the symbol table, PHP will add it to the symbol table, and if the symbol table is not full, it will use the applied but unused memory in the symbol table. If the symbol table happens to be full, it will apply for new memory for storage, and the new memory is not only as small as the memory required by this variable.

Thank you for reading this article carefully. I hope the article "Analysis of the principle of PHP garbage Collection Mechanism" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report