The difference between php5 and php7 garbage collection
The garbage collection mechanisms of PHP5 and PHP7 both belong to reference counting, but in complex data types Algorithm processing: There is a new implementation of zval in PHP7.
The most basic change is that the memory required by *zval is no longer allocated separately from the heap, and the reference count is no longer stored by itself. The reference count of complex data types (such as strings, arrays, and objects) is stored by itself.
This implementation has the following benefits:
1. Simple data types do not need to allocate memory separately, and do not need to be counted;
2. There will not be two more times counting situation. In an object, only the count stored in the object itself is valid;
3. Since the count is now stored by the value itself, it can be shared with data in non-zval structures, such as between zval and hashtable key.
What is reference counting?
Since PHP is written in C, there is something called a structure in C. Our PHP variables are stored in this way in C.
Each PHP variable exists in a container called zval. A zval container, in addition to the variable name and value, also includes two bytes of additional information:
● A called 'is_ref' is a Boolean value used to indicate whether this variable belongs to the reference set. Through this byte, we can distinguish ordinary variables from reference variables in PHP.
● The second extra byte is 'refcount', which is used to indicate the number of variables pointing to this container.
Comparison of reference counting between PHP5 and PHP7
php 5.* The reference counting for operations such as variable assignment is as shown in the figure. In the penultimate step, a loop will be formed. Reference, and after the unset operation, garbage will be generated.
The count of PHP 7 is placed in a specific value, and zval does not have copy-on-write (split-on-write).
And PHP 7 has a special zend_reference used to represent references.
The above is the detailed content of What is the difference between php5 and php7 garbage collection?. For more information, please follow other related articles on the PHP Chinese website!