Home >Backend Development >PHP Tutorial >Memory management and garbage collection of PHP scripts
$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.
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:
'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),得到释放
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.
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)
$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.
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),得到释放
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!