Home > WeChat Applet > Mini Program Development > Detailed explanation of PHP memory release and garbage collection

Detailed explanation of PHP memory release and garbage collection

小云云
Release: 2018-03-30 10:00:00
Original
2462 people have browsed it

This article mainly shares with you tips on writing programs in PHP, as well as issues related to memory release and garbage collection. Friends who are interested can learn from it. I hope it can help everyone.

Reference assignment


$a = 'apple';
$b = &$a;
Copy after login

In the above code, I assign a string to the variable a, and then assign the reference to a Variable b is given. Obviously, the memory pointing at this time should be like this:


$a -> &#39;apple&#39; <- $b
Copy after login

a and b point to the same memory area, we get string through var_dump($a, $b) (5) "apple" string(5) "apple" , this is the result we expected.

unset function

Suppose I want to release the string 'apple' from memory. This is what I did:


unset($a);
Copy after login

But by printing the information of the two variables $a $b again, I got this result: Notice: Undefined variable: a and string (5) "apple". Strangely, $a and $b both point to a memory area, and $a is clearly released. Why is $b still 'apple'.

Actually, unset() destroys a variable pointer and does not release the string stored in that memory area. Therefore, after the operation is completed, the memory pointer just becomes like this :


&#39;apple&#39; <- $b
Copy after login

Important points to remember: unset() does not release the memory pointed to by the variable, but only destroys the variable pointer. At the same time, the reference count of that piece of memory is decremented by 1. When the reference count is 0, that is to say, when that piece of memory is not referenced by any variable, PHP's garbage collection will be triggered.

Direct recycling

So what can be done to really release the memory occupied by 'apple'?

Using the above method, we can unset($a) and then unset($b) to destroy all references in the memory area. When the reference count is reduced to 0, it will naturally be recycled by PHP.

Of course, there is a more direct method:


$a = null;
Copy after login

Directly assigning null will empty the memory area pointed to by $a and replace the reference. The count returns to zero and the memory is released.

End of script execution

php is a scripting language. When the script execution ends, all the memory used in the script will be released.

Related recommendations:

Master PHP garbage collection mechanism

##PHP built-in garbage collection

Detailed explanation of PHP’s garbage collection mechanism


The above is the detailed content of Detailed explanation of PHP memory release and garbage collection. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template