Memory management and garbage collection of PHP scripts

不言
Release: 2023-03-22 17:42:02
Original
1570 people have browsed it

This article shares with you about the memory management and garbage collection of PHP scripts. It shows you the memory management and garbage collection of PHP scripts through examples. Friends in need can refer to it.

Reference assignment

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

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

$a -> 'apple' <- $b
Copy after login

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


Reference counting and unset( )

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

unset($a);
Copy after login
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". Strange, $a $b points to a memory area at the same time, and clearly releases $a, why $b is still 'apple'.

Actually, this is the case, unset()This destroys a variable pointer and does not release the memory area (variable container), so after the operation is completed, the memory points to It just becomes like this:

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

The important thing to remember: unset() does not release the memory (variable container) pointed to by the variable, but only destroys the variable pointer. At the same time, reduce the reference count (ref count) of that piece of memory by 1. When the reference count is 0, that is to say, when that piece of memory (variable container) is not referenced by any variable, it will be triggered. PHP garbage collection.

Use the code to verify:

$a = &#39;apple&#39;;
$b = &$a;

$before = memory_get_usage();
unset($a);
$after = memory_get_usage();

var_dump($before - $after);  // 结果为int(0),没有释放
Copy after login
$a = &#39;apple&#39;;
$b = &$a;

$before = memory_get_usage();
unset($a, $b);
$after = memory_get_usage();

var_dump($before - $after);  // 结果为int(24),得到释放
Copy after login

Direct recycling

What should we do to really release it'apple' What about the memory occupied?

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

Of course, there is a more direct method:

$a = null;
Copy after login

Direct assignment null will empty the memory area pointed to by $a and Reset the reference count to zero and the memory is released.


End of script execution

php is a scripting language. When the script execution ends, all memory used in the script will be released. So, does it make sense for us to manually release the memory? In fact, there has been an answer to this question for a long time. I recommend everyone to read an article published by Brother Bird @laruence in 2012: Please release resources manually (Please release resources manually)

citation assignment

$a = &#39;apple&#39;;
$b = &$a;
Copy after login

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

$a -> 'apple' <- $b
Copy after login

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


Reference counting and unset( )

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

unset($a);
Copy after login
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". Strange, $a $b points to a memory area at the same time, and clearly releases $a. Why is $b still 'apple'.

Actually, this is the case, unset()This destroys a variable pointer and does not release the memory area (variable container), so after the operation is completed, the memory points to It just becomes like this:

'apple' <- $b
Copy after login

The important thing to remember: unset() does not release the memory (variable container) pointed to by the variable, but only destroys the variable pointer. At the same time, reduce the reference count (ref count) of that piece of memory by 1. When the reference count is 0, that is to say, when that piece of memory (variable container) is not referenced by any variable, it will be triggered. PHP garbage collection.

Use the code to verify:

$a = 'apple';
$b = &$a;

$before = memory_get_usage();
unset($a);
$after = memory_get_usage();

var_dump($before - $after);  // 结果为int(0),没有释放
Copy after login
$a = 'apple';
$b = &$a;

$before = memory_get_usage();
unset($a, $b);
$after = memory_get_usage();

var_dump($before - $after);  // 结果为int(24),得到释放
Copy after login

Direct recycling

What should we do to really release it'apple' What about the memory occupied?

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

Of course, there is a more direct method:

$a = null;
Copy after login

Direct assignment null will empty the memory area pointed to by $a and Reset the reference count to zero and the memory is released.

The above is the detailed content of Memory management and garbage collection of PHP scripts. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!