Home > Backend Development > PHP7 > body text

What is the difference between the garbage collection mechanisms of PHP5 and PHP7?

醉折花枝作酒筹
Release: 2023-02-18 06:58:01
forward
1869 people have browsed it

The garbage collection mechanisms of php5 and php7 all use 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. Today we will learn the difference between the garbage collection mechanisms of PHP5 and PHP7.

What is the difference between the garbage collection mechanisms of PHP5 and PHP7?

The garbage collection mechanisms of php5 and php7 both use reference counting

Let’s first take a look at what reference counting is:

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 structure called Among the zval containers, a zval container not only contains the variable name and value, but also includes two bytes of additional information. One is called 'is_ref', which is a Boolean value used to indicate whether the variable belongs to the reference set. Through this byte, Only in PHP can we distinguish ordinary variables from reference variables. The second extra byte is 'refcount', which is used to indicate the number of variables pointing to this container

We define a variable in PHP

$name='看看';
Copy after login

We can now use the xdebug_debug_zval() function to obtain relevant information about the variables within the function

xdebug_debug_zval('name');
//输出 name:(refcount=1,is_ref=0)='new String'
现在我们把$name赋值给$money
$money = $name;
Copy after login

Let’s check again:

xdebug_debug_zval('name');
//输出 name:(refcount=2,is_ref=0)='new String'
Copy after login

At this time we will see the value of the refcount field Added 1, which means that the two existing variables point to the same aval container named name

According to the rules of reference counting, when the refcount=0, PHP will treat this container as garbage. Recycling.

Similarly we execute

unset($name); //一样也会把name容器的引用计数设置为0
Copy after login

The above conclusion is only for scalar types. Let’s take a look at the

array responsible for the type:

$person=['name'=>'请欢','age'=>19];
var_dump(xdebug_debug_zval('person'));
在PHP5输出:
person:
(refcount=1, is_ref=0),
array (size=2)
  'name' => (refcount=1, is_ref=0),string '看看' (length=6)
  'age' => (refcount=1, is_ref=0),int 19
Copy after login

Output in PHP7:

person:
(refcount=2, is_ref=0)
array (size=2)
  'name' => (refcount=1, is_ref=0)string '看看' (length=6)
  'age' => (refcount=0, is_ref=0)int 19
Copy after login

It can be seen that for complex data types, the reference counting algorithms of PHP5 and PHP7 are different

Let’s try a loop Quotation

Based on the above code, add a line of code

$person['hello'] = $person['name']
Copy after login

Output in PHP7:

person:
(refcount=1, is_ref=0)
array (size=3)
  'name' => (refcount=3, is_ref=0)string '看看' (length=6)
  'age' => (refcount=0, is_ref=0)int 19
  'hello' => (refcount=3, is_ref=0)string '看看' (length=6)
Copy after login

Output in PHP5:

person:
(refcount=1, is_ref=0),
array (size=3)
  'name' => (refcount=2, is_ref=0),string '看看' (length=6)
  'age' => (refcount=1, is_ref=0),int 19
  'hello' => (refcount=2, is_ref=0),string '看看' (length=6)
Copy after login

Short summary:

The garbage collection mechanisms of PHP5 and PHP7 both belong to reference counting, but in terms of algorithm processing of complex data types:

In PHP7, zval has a new implementation. 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: *

*Simple data types do not need to allocate memory separately, and do not need to be counted; *

*There will be no more double counting. In an object, only the count stored in the object itself is valid; *

*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; *

Recommended learning: php video tutorial

The above is the detailed content of What is the difference between the garbage collection mechanisms of PHP5 and PHP7?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template