search
HomeBackend DevelopmentPHP7What is the difference between php7 garbage collection and php5

What is the difference between php7 garbage collection and php5

Oct 18, 2019 pm 03:17 PM
php5php7the differenceGarbage collection

What is the difference between php7 garbage collection and php5

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

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 containing the variable name and value, also includes two bytes of additional information. One is called 'is_ref', which is a Boolean value. , used to indicate whether this variable belongs to a reference collection. Through this byte, we can PHP 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. number.

Output in 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

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

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

Let’s try the circular reference situation:

Based on the above code, add a line of code

$person['hello'] = $person['name']

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)

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)

Summary:

The garbage collection mechanisms of PHP5 and PHP7 both belong to reference counting. But in terms of algorithm processing of complex data types: zval has a new implementation 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 require counting;

2. There will be no more double counting. 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.

Recommended tutorial: PHP7 tutorial

The above is the detailed content of What is the difference between php7 garbage collection and php5. 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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment