What are the contents related to PHP garbage collection and memory management
This article mainly explains the "PHP garbage collection and memory management related content", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "PHP garbage collection and memory management related content" bar!
Guidelines for PHP garbage Collection and memory Management
This article will talk about garbage collection and memory management in the development of PHP.
Reference count
In PHP 5.2 and previous versions, PHP garbage collection used the reference counting algorithm.
Basic knowledge of reference counting
Basic knowledge of reference counting
The variables for php are stored in the "zval" variable container (data structure), and the "zval" attribute contains the following information:
The data type of the current variable
The value of the current variable
Is_ref Boolean type identification used to identify whether a variable is passed by a reference
The refcount identifier that points to the number of variables in the "zval" variable container (that is, the number of times this zval is referenced, note that the reference here does not refer to the reference passing value, note the distinction).
When a variable is assigned, a corresponding "zavl" variable container is generated.
View variable zval container information
To view the "zval" container information of a variable (that is, to view the is_ref and refcount of a variable), you can use the xdebug_debug_zval () function of the XDebug debugger.
Assuming that we have successfully installed the XDebug tool, we are ready to debug the variables.
View the zval information of ordinary variables
If our PHP statement simply assigns a variable, the is_ref identification value is 0 and the refcount value is 1; if this variable is assigned to another variable as a value, the refcount count of the zval variable container is increased; similarly, when the variable is destroyed (unset), the corresponding "refcount" minus 1.
Take a look at the following example: