Home >Backend Development >PHP Tutorial >Memory management and garbage collection of PHP scripts

Memory management and garbage collection of PHP scripts

不言
不言Original
2018-05-30 09:57:411672browse

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;

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

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);

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

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),没有释放
$a = &#39;apple&#39;;
$b = &$a;

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

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

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;

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;

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

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);

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

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),没有释放
$a = 'apple';
$b = &$a;

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

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

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;

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!

Statement:
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