PHP garbage collection mechanism

Source: Internet
Author: User
PHP can automatically manage the memory and clear unnecessary objects. PHP uses the referencecounting (garbagecollection) mechanism

PHP can automatically perform memory management to clear unnecessary objects. PHP uses reference counting, a simple garbage collection mechanism. each object contains a reference counter. each reference is connected to an object, and the counter is incremented by 1. when the reference leaves the living space or is set to NULL, the counter minus 1. when the reference counter of an object is zero, PHP knows that you no longer need to use this object, releasing the memory space occupied by it.

The garbage collection mechanism used before php 5.3 is a simple "reference count", that is, each memory object is allocated with a counter. when the memory object is referenced by a variable, the counter is + 1; when the variable reference is removed, counter-1; when counter = 0, indicates that the memory object is not used, the memory object is destroyed, and garbage collection is completed.

There is a problem with "reference count", that is, when two or more objects reference each other to form a ring, the counter of the memory object will not be reduced to 0; at this time, this group of memory objects is useless, but cannot be recycled, resulting in memory leakage. php5.3 started with the new garbage collection mechanism, based on the reference count, a complex algorithm is implemented to detect the existence of reference rings in memory objects to avoid memory leakage.

The php variable exists in a variable container named "zval". The "zval" variable container includes the variable type and value, and contains two additional bytes, "is_ref" indicates whether the variable is referenced, and "refcount" indicates the number of variables in the zval variable container.

If you have installed xdebug, you can use xdebug_debug_zval () to display "zval" information as follows:

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.