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!