PHP garbage collection mechanism

PHP garbage collection mechanism

PHP garbage collection mechanism

1. When each variable is defined, it is stored in a container called zval, which contains the type and value of the quantity, and also contains a refcount (understood as the number of variables) and is_ref (understood as whether it is Reference variable) Two additional information. When the variable is referenced once, the refcount will be +1. When you unset it, the value will be reduced by 1 until it reaches 0 and it will be deleted from the memory.

2. When you define a variable, you do not expand the predetermined value every time, because PHP will pre-occupy a space in memory, and it will be allocated to you when you declare the variable, but when you exceed the pre-occupied space , Then it will increase the space, but this space capacity will not disappear immediately when you delete the variable

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

4. Simply put, PHP variables depend on an internal implementation

symbol_table symbol table, and the basic implementation of symbol table is HashTable

, Which is consistent with the basic implementation of PHP arrays. It's really because of the existence of the symbol table that we can use global to mark global variables, and use functions such as compact to directly pull variables out of the current symbol table.

Then I'm 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 mark this space again available) Instead of freeing up space).

Let's talk about the situation of repeatedly updating $key in the loop. Because the variables with the same name are updated, they are the same element in the symbol table. The same position will be updated during the update, and the value of the previous element will be overwritten immediately.

Let’s talk about the problem of declaring that new variable memory will increase. The answer is indeterminate. This is the symbol table based on

Due to the characteristics of HashTable implementation, HashTable

It is not to add an element to apply for the memory of one element, but to apply for the memory of multiple elements at a time (only these position marks are unused), and when HashTable

When it is full, apply for a new memory for multiple elements. That is to say, 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 at this time, it will use the application in the symbol table but not used If the symbol table is just full, new memory will be applied for storage, and the new memory is not only as small as the memory required by this variable

Reference: https://cloud.tencent.com/developer/article/1080602 PHP Garbage Collection Mechanism-Cloud + Community-Tencent Cloud