Php reference count

Source: Internet
Author: User

The garbage collection mechanism used before php5.3 is "reference count ".

The principle of "reference count" is that each memory object is allocated with a counter. When the memory object is referenced by a variable, the counter + 1; when the variable reference is removed, the counter-1; when the counter is 0, it 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; this will cause memory leakage.

Now let's take a look at several operations related to PHP memory release:

1. & Symbol impact
Explicitly referencing a variable increases the reference count of the memory:
$ A = "something ";
$ B = & $;

At this time, unset ($ a), but there is still a reference of $ B pointing to the memory area, the memory will not be released.

2. Impact of unset Functions
Unset only disconnects a variable from A memory area, and calculates the reference count of the memory area-1, but is inside the loop body, for example, $ a = new (); unset ($ a); does not reduce the reference count of $ a to zero;

3. Impact on the null value operation of the variable;
$ A = null: directly empty the data structure pointed to by $ a and return the reference count to 0.

4. Impact of script execution completion
After the script is executed, all memory used in the script will be released, regardless of whether there is a reference ring.

So the above introduces so much memory-related things, but the memory is recycled by the PHP garbage collection process. Of course, you can also manually recycle the process like writing C and assign null values to the variables used. However, this will increase the complexity of the program, but it is generally not recommended for PHP. If, as mentioned in your Q & A, it is in the code that processes a large amount of code and runs for a long time, it is better to manually destroy some unused variables. You can also use functions such as memory_get_usage and memory_get_peak_usage to monitor memory usage.

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.