PHP garbage collection mechanism use case analysis

Source: Internet
Author: User
This time to bring you a PHP garbage collection mechanism use case analysis, PHP garbage collection mechanism to use the attention of what, the following is the actual case, together to see.

1, each variable definition is stored in a container called Zval, which contains the number of types and values, but also contains a refcount (understanding that there are several variables) and Is_ref (understood as a reference variable) two additional information, When a variable is quoted once RefCount will be +1, and when you unset it, the value will be reduced by 1 until 0 will be removed from memory.

2, the time to define a variable does not always expand the predetermined value, because PHP will pre-occupy a space in memory, when you declare the variable will be assigned to you, but when you exceed the pre-occupied space, then 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 be a separate increase in memory consumption, it will point to the zval structure, just refcount+1

4, in short, the PHP variable depends on an internal implementation of the symbol_table symbol table, and the basic implementation of the symbol table is HashTable, that is, and the basic implementation of the PHP array is consistent. It is because of the existence of the symbol table that we can use the global tag to pull variables directly from the current symbol table using functions such as compact.

That unset ($a) will not immediately release the space, the answer is no, unset support from the symbol table to the name of the element of A is deleted (just mark the space is available, rather than free space).

Again in the loop, this is the case of repeating update $key, because the update is the same name of the variable, so in the symbol table they are the same element, the update will update the same position, the value of the previous element is immediately overwritten.

And say that the new variable memory will increase the problem, the answer is not sure. This is caused by the HashTable implementation of the symbol table, HashTable not adding an element to the memory of an element, but the memory of multiple elements at a time (except that the location tag is unused), and when the HashTable is stuffed, then the memory of the new multiple elements is requested. In other words, when we declare or assign a variable, if it is not in the symbol table, PHP will add it to the symbol list, and if this time the symbol table is not full, it will be used in the symbol table in the application of unused memory, if the symbol table is just full, will request new memory to store, And the new memory is more than just this variable that requires so little memory.

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

PHP+OPENSSL extension implementing public key encryption steps

PHP RSA ciphertext encryption and decryption steps detailed

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.