Let's talk about the garbage collection mechanism of PHP and the garbage collection mechanism of php.

Source: Internet
Author: User

Let's talk about the garbage collection mechanism of PHP and the garbage collection mechanism of php.

1. Every variable definition is saved in a container named zval. Here, the type and value of the quantity are included, and a refcount is included. (several variables exist) and is_ref (understood as whether it is a referenced variable). When the variable is referenced for a refcount operation, after unset, this value will be reduced by 1 until it is 0 and will be deleted from the memory.

2. Defining a variable does not always increase the reserved value, because PHP will pre-occupy a space in the memory and will assign it to you when declaring the variable, but when you exceed the pre-occupied space, it will increase the space, but the space will not disappear immediately when you delete the variable.

3. variable reference does not increase memory usage separately. It points to the zval struct, but refcount + 1

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

The answer is no if the unset ($ a) mentioned by the subject will immediately release space, unset supports deleting the element named a from the symbol table (only marking the space is available, rather than releasing the space ).

In this case, the $ key is updated repeatedly in the loop. Because the variables with the same name are updated, they are the same element in the symbol table, and the same position will be updated during the update, the value of the previous element is immediately overwritten.

In addition, if the memory of the new variable is affirmed, this problem will be added. The answer is uncertain. This is because the symbolic table is implemented based on HashTable. HashTable does not apply for the memory of an element if it is added, instead, the memory of multiple elements is applied at a time (only these locations are not used), and when HashTable is full, the memory of the new multiple elements is applied. 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. If the symbol table is not full at this time, the memory that has been applied for but not used in the symbol table will be used. If the symbol table is just full, a new memory will be applied for storage, the new memory is not only as small as the memory required by this variable

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.