Brother Bird's blog is a big treasure, is looking at from the beginning.
"In-depth understanding of PHP memory management who moved my memory"
1. Memory_get_usage function
int Memory_get_usage ([bool $real _usage = false])
Memory used by PHP scripts (without the memory of the Memory_get_usage () function itself)
Memory_get_usage () parameter $real_usage, default is FALSE, and when set to TRUE, the resulting memory value is the memory occupied by PHP memory management.
PHP allocated to the memory Var_dump (Memory_get_usage (true));//php uses internal memory var_dump (Memory_get_usage ());
PHP's memory management mechanism is: pre-allocated a piece of space, used to store variables, when the space is not enough, then apply for a new space.
Memory_get_usage (True) is the pre-allocated memory.
I didn't pay attention to the memory usage of the PHP page before, and we should use this function to analyze the code after the page optimization.
2. Variable assignment and memory
Var_dump (Memory_get_usage ()); $a = "Hello world"; Var_dump (Memory_get_usage ()); $b = $a; Var_dump (Memory_get_usage ()); Unset ($b); Var_dump (Memory_get_usage ()); unset ($a); Var_dump (Memory_get_usage ());
The amount of memory used by the $a and $b assignment is different because the $b assignment does not allocate memory, which is consumed by the output function. 88 bytes.
In addition, destroying $b does not affect the memory used by $ A, which is why memory management reference counts
Changing the example will give you a clear understanding of the relationship between variable assignment and memory.
Var_dump (Memory_get_usage ()); $a = "Hello World", Var_dump (Memory_get_usage ()); $b = "Hello World"; Var_dump (memory_get_ Usage ()); unset ($b); Var_dump (Memory_get_usage ()); unset ($a); Var_dump (Memory_get_usage ());
In addition, reference assignment takes on memory rules and is similar to direct assignment, which also involves reference counting rules for memory management
3, PHP garbage collection mechanism, this part of the understanding is not deep enough, simply record a function
Xdebug_debug_zval need to install xdebug extension
$a = "Hello world"; Xdebug_debug_zval (' a ');
(Refcount=1, is_ref=0), String ' Hello world ' (length=11)
RefCount number of variables pointing to this value
Is_ref represents whether there is an address reference
Variable assignment: Is_ref is false refcount is 1
Basic knowledge of reference counting
Each PHP variable exists in a variable container called "Zval". A Zval variable container that includes two bytes of extra information in addition to the type and value of the variable. The first is "Is_ref", which is a bool value that identifies whether the variable belongs to a reference collection (reference set). With this byte, the PHP engine can differentiate between normal and reference variables, and since PHP allows users to use the custom reference by using &, there is an internal reference counting mechanism in the Zval variable container to optimize memory usage. The second extra byte is "RefCount", which represents the number of variables (also known as symbols) that point to the Zval variable container. All symbols exist in a symbol table, where each symbol has scope (scope), and those main scripts (for example, scripts that are requested by the browser) and each function or method also have scopes.
Above to look at the bird Brother's 11 blog article, the PHP variable and memory management has a preliminary understanding.
PHP Note variables and memory management