In-depth analysis of PHP garbage collection mechanism for memory leakage

Source: Internet
Author: User
This article provides a detailed analysis of the PHP garbage collection mechanism for memory leakage. For more information, see refcount and is_ref.

The code is as follows:


$ A = array (1, 2, & $ );
Unset ($ );


In the old PHP version, memory leakage occurs. the analysis is as follows:

Run the first line to know the zval refcount = 2 and is_ref = 1 pointed to by $ a and $ a [2 ].

Then execute the second row. $ a will be deleted from the symbol table and directed to the zval refcount --. in this case, refcount = 1 because refcount! = 0, so zval will not be treated as garbage collection, but at this time we lose the entry of $ a [2] pointing to this zval, so this zval becomes a piece of memory garbage

The same principle can occur in internal class references, for example

The code is as follows:


$ A = new Man ();
$ A-> self = & $;
Unset ($ );


So how can we solve this problem? The New GC mechanism uses an algorithm to solve this problem.

PHP has a root buffer used to store zval node information. when the root buffer is full or the gc function is manually called, the GC algorithm starts.

For an array or zval of the class type, when the garbage collection mechanism is enabled, the algorithm will traverse the zval of elements/members in the zval array/class and reduce refcount by 1. if the refcount of the zval is reduced to 0 after the traversal is complete, this zval is a memory spam and will be destroyed. See the following example.

The code is as follows:


$ A = array (1, 2, & $ a, & $ );
Unset ($ );


It is easy to know the zval pointed to by $ a. assume that refcount of z1 is 3, and is_ref is 1.

When unset ($ a) is executed, $ a has been deleted from the symbol table, and we also lose the entry to access z1. at this time, z1 refcount = 2, is_ref = 1

When GC is started, the zval refcount of the array element of the z1 will be traversed minus 1 and traversed to a [2], z1 refcount --, a [3] z1 refcount --, at this time z1 refcount = 0, you can mark z1 as memory garbage, and the algorithm will recycle it

To sum up, it can be expressed as follows: if an array type zval is used to traverse its element zval, the refcount of the zval will be traversed at the same time. if the last refcount = 0 zval, it is garbage and needs to be recycled.

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.