Home >Backend Development >PHP Problem >What is PHP memory management mechanism and garbage collection mechanism? How to understand it?

What is PHP memory management mechanism and garbage collection mechanism? How to understand it?

王林
王林forward
2019-08-24 10:14:462509browse

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(&#39;a&#39;);echo PHP_EOL;

What is PHP memory management mechanism and garbage collection mechanism? How to understand it?

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(&#39;a&#39;);echo PHP_EOL;

What is PHP memory management mechanism and garbage collection mechanism? How to understand it?

##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(&#39;a&#39;);echo PHP_EOL;
xdebug_debug_zval(&#39;b&#39;);echo PHP_EOL;

What is PHP memory management mechanism and garbage collection mechanism? How to understand it?

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 recycled

2. The garbage collection mechanism has been improved in versions after 5.3

If it is found that the refcount in a zval container is increasing , indicating that it is not garbage

If 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 collection

If 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 up

For more related questions, please visit the PHP Chinese website:

PHP practical video tutorial

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!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete