Home > Backend Development > PHP Tutorial > Analysis on PHP memory release and garbage collection

Analysis on PHP memory release and garbage collection

不言
Release: 2023-04-01 15:02:02
Original
1781 people have browsed it

This article shares with you some 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.

Reference assignment

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

In the above code, I assign a string to variable a, and then a The reference of is assigned to variable b. 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 pass var_dump($a, $b ) gets string(5) "apple" string(5) "apple" , which is what 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 the reference count is reset 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.

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

About analysis of PHP’s move_uploaded_file() function

About var_export, print_r and var_dump in PHP The difference in debugging

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

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