Home  >  Article  >  Backend Development  >  What is the difference between php5 and php7 garbage collection?

What is the difference between php5 and php7 garbage collection?

青灯夜游
青灯夜游Original
2019-10-19 15:49:293872browse

What is the difference between php5 and php7 garbage collection?

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.

What is the difference between php5 and php7 garbage collection?

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.

What is the difference between php5 and php7 garbage collection?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Is php a web page file?Next article:Is php a web page file?