Analysis of PHP memory release and garbage collection

Source: Internet
Author: User
This article gives you a little tips on how to write programs in PHP, about memory releases and garbage collection issues, and interested friends to learn.

Reference assignment

$a = ' Apple '; $b = & $a;

In the code above, I assign a string to the variable a, and then assign a reference to the variable B. Obviously, this time the memory point should be like this:

$a ' apple ' <-$b

A and B point to the same area of memory, and we get the string (5) "Apple" string (5) "Apple" via Var_dump ($a, $b), which is what we expected.

unset function

If I want to release the ' Apple ' string from memory. That's what I did:

unset ($a);

But by printing $a $b Two variables again, I get the result: notice:undefined variable:a and string (5) "Apple". Strangely, $a $b pointing to a memory area and releasing $ A, why $b is still ' apple '.

In fact, unset () This is a variable pointer is destroyed, and does not release the memory area of the stored string, so after performing the operation, the memory point just become this:

' Apple ' <-$b

Keep in mind the point: unset () does not release the memory that the variable points to, but only destroys the variable pointer. At the same time, the reference count of that memory is reduced by 1, when the reference count is 0 o'clock, that is, when the block of memory is not referenced by any variable, it triggers a garbage collection of PHP.

Direct Recycling

What can be done to really release the memory that ' Apple ' occupies?

Using the above method, we can unset ($b) after unset ($a), destroy all references in the memory area, reduce the reference count to 0, and naturally be recycled by PHP.

There is, of course, a more straightforward approach:

$a = null;

A direct assignment of NULL will empty the area of memory that the $a points to, and the reference count will be zeroed, and memory will be freed.

End of script execution

PHP is a scripting language, and when the script executes, all memory used within the script is freed.

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

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.