PHP garbage collection mechanism

Source: Internet
Author: User
PHP garbage collection mechanism understanding this article mainly discusses the differences between PHP5.2 and 5.3 .? 5.2 The "reference count" method is used for recovery. Simply put, there is a counter in each allocated memory area, recording how many Variable pointers point to this memory. When the number of pointers pointing to the memory of the video is 0, the memory area of the video can be recycled .? The reference counting is simple and powerful, but there is a fatal defect, that is, ring reference. Consider the following code :? $ Understanding of PHP garbage collection mechanism

This article mainly discusses the differences between the garbage collection mechanism of PHP5.2 and 5.3.

?

5.2 The "reference count" method is used for recovery. Simply put, there is a counter in each allocated memory area, recording how many Variable pointers point to this memory. When the number of pointers pointing to the memory of the video is 0, the memory area of the video can be recycled.

?

The reference counting is simple and powerful, but there is a fatal defect, that is, ring reference. Consider the following code:

?

$a = array();$a[] = &$a;unset($a);
?


Variable $ a references itself to form a ring. $ A is unset, but due to the existence of a ring reference, the reference count of memory pointed to before $ a is 1, so the memory area will not be reclaimed by the garbage collection mechanism.

?

?

PHP5.3 optimizes this major defect. Although it is still based on reference counting, some improvements have been made to control the memory leakage caused by ring references to a certain scale. Of course, this does not mean that you can abuse the memory at will, and you should be careful when writing code!

?

?

Other highlights:

1. after the PHP script is run, all memory space applied by the script will be released, regardless of whether there is a ring reference. Therefore, the memory leakage caused by ring references generally only affects scripts that run for a long time.

?

2. the garbage collection mechanism must meet certain conditions before execution. Therefore, after unset, the system does not necessarily recycle the garbage immediately.

?

3. Functions of unset.

"Unset is onlyDisconnectedA variable is connected to a memory area, and the reference count of the memory area is-1 ". That is to say, if more than one variable points to the same memory area or has a ring reference, unset will not release the memory area.DisconnectedIt also indicates that unset does not directly delete the memory area, but only changes its reference count.

?

4. $ xx = null.

"$ A = null is to directly empty the data structure pointed to by $ a and return its reference count to 0 ". According to my understanding of this definition, the = null operation can immediately release the memory space! As a result, many PHP skills are reluctant to say to us that we should first set the variable to null and then unset. After understanding its deep principles, I fully understood the reasons for doing so! = Null is King!

?

?

?

For more information, see:

Http://wenku.baidu.com/view/564573c62cc58bd63186bd35.html

Http://apps.hi.baidu.com/share/detail/46642891

?

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.