Home >Backend Development >PHP Problem >What is PHP memory management mechanism and garbage collection mechanism? How to understand it?
PHP memory management mechanism
var_dump(memory_get_usage()); //获取内存 $a = "laruence"; //定义一个变量 var_dump(memory_get_usage()); //定义变量之后获取内存 unset($a); //删除该变量 var_dump(memory_get_usage()); //删除变量后获取内存
Output result:
int 262144 int 262144 int 262144
It can be seen from the above that PHP’s memory management mechanism is: pre-given Create a space to store variables. When the space is not enough, apply for a new space.
1. Store variable names and have symbol tables.
2. Variable values are stored in memory space.
3. When deleting a variable, the space where the variable value is stored will be released, but the symbol table where the variable name is located will not be reduced.
var_dump(memory_get_usage()); //获取内存 //定义100个变量 for($i=0;$i<100;$i++) { $a = "test".$i; $$a = "hello"; } //获取定义100个变量之后的内存 var_dump(memory_get_usage()); //定义100个变量并删除 for($i=0;$i<100;$i++) { $a = "test".$i; unset($$a); } //获取删除之后的内存 var_dump(memory_get_usage());
Output result:
int 240040 int 257680 int 240856
As can be seen from the above, although the memory becomes smaller after deletion, it is still larger than before the variable was defined. This is because Although the value of the variable is deleted, the variable name is not deleted.
php garbage collection mechanism
PHP variable storage is stored in a zval container
1. Type 2. Value 3. is_ref represents whether there is an address reference 4. refcount Number of variables pointing to the value
1. When assigning values to variables: is_ref is false and refcount is 1
$a = 1; xdebug_debug_zval('a');echo PHP_EOL;
2. Assign the value of variable a to Variable b, variable b will not store the value in the memory immediately, but first points to the value of variable a until there is any operation on variable a
$b = $a; xdebug_debug_zval('a');echo PHP_EOL;##3 .Because the program operates variable a again, variable b will apply for a piece of memory and put the value in it. Therefore, the refcount in the zavl container of variable a will decrease by 1 and become 1. The variable c points to a, so the refcount will increase by 1 and become 2.
$c = &$a; xdebug_debug_zval('a');echo PHP_EOL; xdebug_debug_zval('b');echo PHP_EOL;
Garbage collection:
1. In version 5.2 or earlier, PHP will determine whether it is garbage based on the refcount value. If the refcount value is 0, PHP will release it as garbage.This recycling mechanism is defective, and variables with circular references cannot be recycled2. The garbage collection mechanism has been improved in versions after 5.3If it is found that the refcount in a zval container is increasing , indicating that it is not garbageIf it is found that the refcount in a zval container is decreasing, if it is reduced to 0, it will be directly treated as garbage collectionIf it is found that the refcount in a zval container is decreasing, it has not been reduced. to 0, PHP will put the value in the buffer as a possible suspect object of garbage. When the buffer reaches the critical value, PHP will automatically call a method to traverse each value. If it is found to be garbage, clean it upFor more related questions, please visit the PHP Chinese website:The above is the detailed content of What is PHP memory management mechanism and garbage collection mechanism? How to understand it?. For more information, please follow other related articles on the PHP Chinese website!