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

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

Jul 02, 2021 am 09:13 AM
php5 php7

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
What is the difference between php5 and php8 What is the difference between php5 and php8 Sep 25, 2023 pm 01:34 PM

The differences between php5 and php8 are in terms of performance, language structure, type system, error handling, asynchronous programming, standard library functions and security. Detailed introduction: 1. Performance improvement. Compared with PHP5, PHP8 has a huge improvement in performance. PHP8 introduces a JIT compiler, which can compile and optimize some high-frequency execution codes, thereby improving the running speed; 2. Improved language structure, PHP8 introduces some new language structures and functions. PHP8 supports named parameters, allowing developers to pass parameter names instead of parameter order, etc.

What should I do if the plug-in is installed in php7.0 but it still shows that it is not installed? What should I do if the plug-in is installed in php7.0 but it still shows that it is not installed? Apr 02, 2024 pm 07:39 PM

To resolve the plugin not showing installed issue in PHP 7.0: Check the plugin configuration and enable the plugin. Restart PHP to apply configuration changes. Check the plugin file permissions to make sure they are correct. Install missing dependencies to ensure the plugin functions properly. If all other steps fail, rebuild PHP. Other possible causes include incompatible plugin versions, loading the wrong version, or PHP configuration issues.

How to change port 80 in php5 How to change port 80 in php5 Jul 24, 2023 pm 04:57 PM

How to change port 80 in php5: 1. Edit the port number in the Apache server configuration file; 2. Edit the PHP configuration file to ensure that PHP works on the new port; 3. Restart the Apache server, and the PHP application will start running on the new port. run on the port.

How to install mongo extension in php7.0 How to install mongo extension in php7.0 Nov 21, 2022 am 10:25 AM

How to install the mongo extension in php7.0: 1. Create the mongodb user group and user; 2. Download the mongodb source code package and place the source code package in the "/usr/local/src/" directory; 3. Enter "src/" directory; 4. Unzip the source code package; 5. Create the mongodb file directory; 6. Copy the files to the "mongodb/" directory; 7. Create the mongodb configuration file and modify the configuration.

How to solve the problem when php7 detects that the tcp port is not working How to solve the problem when php7 detects that the tcp port is not working Mar 22, 2023 am 09:30 AM

In php5, we can use the fsockopen() function to detect the TCP port. This function can be used to open a network connection and perform some network communication. But in php7, the fsockopen() function may encounter some problems, such as being unable to open the port, unable to connect to the server, etc. In order to solve this problem, we can use the socket_create() function and socket_connect() function to detect the TCP port.

PHP Server Environment FAQ Guide: Quickly Solve Common Problems PHP Server Environment FAQ Guide: Quickly Solve Common Problems Apr 09, 2024 pm 01:33 PM

Common solutions for PHP server environments include ensuring that the correct PHP version is installed and that relevant files have been copied to the module directory. Disable SELinux temporarily or permanently. Check and configure PHP.ini to ensure that necessary extensions have been added and set up correctly. Start or restart the PHP-FPM service. Check the DNS settings for resolution issues.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to install and deploy php7.0 How to install and deploy php7.0 Nov 30, 2022 am 09:56 AM

How to install and deploy php7.0: 1. Go to the PHP official website to download the installation version corresponding to the local system; 2. Extract the downloaded zip file to the specified directory; 3. Open the command line window and go to the "E:\php7" directory Just run the "php -v" command.

See all articles