First, the memory caching mechanism
Var_dump (Memory_get_usage (true)); $a = "laruence"; Var_dump (Memory_get_usage (true)); unset ($a); Var_dump (memory_get_ Usage (true));
1. Plus true, the display of the three equal number represents the size of the PHP application memory, whether you are empty or stored in the data, the PHP application to show the memory, when you store the data is too large, PHP will request a new memory to expand, so that the regular memory of PHP calls to avoid.
2. All PHP memory caching mechanism is:PHP does not define a data every time to apply for a memory, but to pre-request a piece of memory, when the storage data is not enough, once again request a piece of memory expansion
3.
$a = "Hello";
1. Stored variable names, which exist in the symbol table.
2. Variable values are stored in memory space
3. When you delete a variable, the space stored by the variable value is freed, and the symbol table that contains the variable name does not decrease
4. For example:
Var_dump (Memory_get_usage ()); for ($i =0; $i <100; $i + +) {$a = "test". $i; $ $a = "Hello";} Var_dump (Memory_get_usage ()); for ($i =0; $i <100; $i + +) {$a = "test". $i; unset ($ $a);} Var_dump (Memory_get_usage ());
Second, the garbage collection mechanism
1.
The PHP variable store is stored in a zval container.
Zval is roughly divided into four pieces.
1. Type 2. Variable Value 3.is_ref Boolean to indicate if there is an address reference 4.refcount points to the number of the worthy variable
2. For example
1. Variable assignment: Is_ref is false RefCount is 1$a=1;xdebug_debug_zval (' a '); Echo PHP_EOL;//2. Assigns the value of variable A to variable B, and variable B does not immediately store the value in memory. Instead, point to the value of variable a until the variable A has any action $b= $a; Xdebug_debug_zval (' a '); Echo PHP_EOL;//3. Because the program also operates the variable A, the variable B will itself apply for a piece of memory to put the value in (b does not point to a, But C points to a again) $c =& $a; Xdebug_debug_zval (' a '); Echo php_eol;xdebug_debug_zval (' B '); Echo php_eol;
3.
Garbage collection:
1. Before the 5.2 version, PHP will determine whether the garbage is based on the Refcunt value
If the RefCount value is 0,php, it will be released as garbage.
This recovery mechanism is defective and cannot be recycled for ring-referenced variables
Ring quote $attr=array ("Hello"), $attr []=& $attr; Xdebug_debug_zval ("attr"); Echo php_eol;
2. After the 5.3 version improved the garbage collection mechanism
If you find that the refcount in a zval container is increasing, the description is not garbage
If the refcount in the Zval container is found to be reduced, if it is reduced to 0, it is directly treated as garbage collection
If the refcount in the Zval container is found to be decreasing, it is not reduced to 0,php will put that value into the buffer, as if there were suspected objects that could be garbage.
When the buffer reaches a critical value, PHP automatically calls a method to iterate through each value, and if it finds garbage, clean it up.
Memory caching mechanism and garbage collection mechanism