The author became interested in this topic a few days ago, so I searched online and found that almost all of them are the garbage collection mechanism of php 5. Although the changes made in the GC part from php5 to php7 are relatively small, I think there are still some You need to make a separate blog post. If not specified specifically, the PHP version is 7.2
The space occupied by variables in PHP does not need to be reclaimed manually. The kernel handles this part of the work for us. Compared with C, this greatly facilitates our operation.
This article mainly explains the GC mechanism of variables
Article directory
- Zval structure
- Memory leak caused by circular reference
- Recycling process of object and array
- Principle of garbage collection
- Example
Recommended (free): PHP7
When understanding our php GC, I feel it is necessary to introduce our php Variables are implemented under the hood.
The structure of zval
// php 变量对于的c结构体 struct _zval_struct { zend_value value; union { …… } u1; union { …… } u2; };
Since it mainly talks about garbage collection, here is a brief introduction to the functions of the u1 u2 unionu1
The structure is relatively complex. I think it is mainly used to identify variable typesu2
Most of them are auxiliary fields, the implementation of internal functions of variables, improving cache friendliness, etc.
Next is us The protagonist
zend_value
It is also a union embedded in the structure
typedef union _zend_value { zend_long lval;//整形 double dval;//浮点型 zend_refcounted *counted;//获取不同类型的gc头部 zend_string *str;//string字符串 zend_array *arr;//数组 zend_object *obj;//对象 zend_resource *res;//资源 zend_reference *ref;//是否是引用类型 // 忽略下面的结构,与我们讨论无关 zend_ast_ref *ast; zval *zv; void *ptr; zend_class_entry *ce; zend_function *func; struct { ZEND_ENDIAN_LOHI( uint32_t w1, uint32_t w2) } ww; } zend_value;
The reference count is recorded in the value of zval
zend_refcounted *counted
This type, our garbage collection mechanism is also based on this.
typedef struct _zend_refcounted_h { uint32_t refcount; /* reference counter 32-bit */ union { struct { ZEND_ENDIAN_LOHI_3( zend_uchar type, zend_uchar flags, /* used for strings & objects */ uint16_t gc_info) /* keeps GC root number (or 0) and color */ } v; uint32_t type_info; } u; } zend_refcounted_h;
The definition of all complex types starts with the zend_refcounted_h
structure. In addition to reference counting, this structure also has GC-related structures. Therefore, when doing GC recycling , GC does not need to care about the specific type, all of it can be processed as a zend_refcounted*
structure.
#Automatic recycling of variables
In PHP except array
and object
type variables, most of the rest are automatically recycled
PHP The recycling of ordinary variables is related to the number of references to the variable.
Official example
$a = 1; $b = $a; xdebug_debug_zval('a'); $a =10; xdebug_debug_zval('a'); unset($a); xdebug_debug_zval('a');
Result
a: (refcount=2, is_ref=0),int 1 a: (refcount=1, is_ref=0),int 10 a: no such symbol
You can see that when $a =10
it involves php’s COW (copy-on- write) mechanism, $b will copy the original $a, canceling the reference relationship between them, so the number of references (refcount) of a is reduced to 1.
Then the number of references to a becomes 0 after we uset($a). This will be considered a garbage variable and free up space.
Give an example
$a = [1]; $a[1] = &$a; unset($a);
The type of $a before unset($a) is a reference type
a: (refcount=2, is_ref=1), array (size=2) 0 => (refcount=1, is_ref=0),int 1 1 => (refcount=2, is_ref=1), &array<p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/052/b5a6578d3bb66f0539d3f3981d5b0923-0.jpg?x-oss-process=image/resize,p_40" class="lazy" alt=""></p><p>unset($ a) After that, it becomes like this</p><p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/052/8e8e7fe4611fd81045139c68c9a2afe3-1.jpg?x-oss-process=image/resize,p_40" class="lazy" alt=""></p><p>At this time, when we <code>unset</code> operate, the refcount changes from 2 to 1 because there is an internal reference pointing to $a , so the space it occupies externally will not be destroyed. </p><p>Then our external reference has been broken and we can't use it. It becomes an "orphan", which is called a wild pointer in the C language. In php it's called a circular reference. Memory leak. If you want to destroy the variable, you can only wait for the php script to end. </p><p><strong>Memory leaks caused by circular references</strong></p><p>In order to clean up this garbage, two criteria are introduced</p>
- If the reference count is reduced to zero, The variable container will be cleared (free) and is not garbage
- If the reference count of a zval is still greater than 0 after being reduced, then it will enter the garbage cycle. Secondly, during a garbage cycle, find out which parts are garbage by checking whether the reference count is reduced by 1 and checking which variable containers have zero references.
Circular references basically only appear in arrays and objects. The object is because it itself is a reference
The recycling process of object and array
Garbage collection in php7 consists of two parts, one is the garbage collector and the other is the garbage collection algorithm.
The garbage collector collects the elements just mentioned that may be garbage into the recycling pool, that is, the variable zend_refcount>0
is placed in the recycling pool. When the value of the recycling pool reaches a certain amount, it will be traversed uniformly. Perform simulated deletion. If zend_refcount=0
, it is considered garbage and deleted directly.
Traverse every variable in the recycling pool, and then traverse each member based on each variable. If the members are still nested, continue traversing. Then set the simulated refcount of all members to -1. If the reference count of the external variable is 0 at this time. Then it can be considered garbage, clearly. If it is greater than 0, then the number of references is restored and taken out from the garbage collection pool.
The principle of garbage collection
If your variable is not garbage, then after the references of all its member variables are reduced by one, the reference of the total variable will definitely not be 0.
example
说的比较死,不如举个例子。刚刷 sf.gg 的时候看到一道关于 GC 的题,我回答了一波。关于GC垃圾回收机制
PHP7 explains the garbage collection mechanism如下
//我的回答 1、只要zval.value的refcount减一,然后缺其refcount的值不为0那么它就可能是垃圾,进入垃圾周期。 2、进入垃圾池遍历所有成员,包括其嵌套的成员,都对其做 refcount-1的操作,看外部的引用是否为0。 那么对于 题主的问题来说, 首先,你要想$a为垃圾,一定要先对 unset($a)操作,那么此时 $a的 refcount = 2 对于$a[0] refcount-1 不影响外部的$a, $a[1] refcount-1 ,此时 $a的 refount=1 $a[2] refcount-1 ,此时 $a 的 refount=0 模拟减结束,那么此变量被当成垃圾回收。
更多免费学习推荐:PHP7教程
The above is the detailed content of PHP7 explains the garbage collection mechanism. For more information, please follow other related articles on the PHP Chinese website!

在php5中,我们可以使用fsockopen()函数来检测TCP端口。这个函数可以用来打开一个网络连接和进行一些网络通信。但是在php7中,fsockopen()函数可能会遇到一些问题,例如无法打开端口、无法连接到服务器等。为了解决这个问题,我们可以使用socket_create()函数和socket_connect()函数来检测TCP端口。

php7.0安装mongo扩展的方法:1、创建mongodb用户组和用户;2、下载mongodb源码包,并将源码包放到“/usr/local/src/”目录下;3、进入“src/”目录;4、解压源码包;5、创建mongodb文件目录;6、将文件复制到“mongodb/”目录;7、创建mongodb配置文件并修改配置即可。

解决 PHP 7.0 中插件未显示已安装问题的方法:检查插件配置并启用插件。重新启动 PHP 以应用配置更改。检查插件文件权限,确保其正确。安装丢失的依赖项,以确保插件正常运行。如果其他步骤均失败,则重建 PHP。其他可能原因包括插件版本不兼容、加载错误版本或 PHP 配置问题。

PHP8相较于PHP7在性能、新特性和语法改进、类型系统、错误处理和扩展等方面都有一些优势和改进。然而,选择使用哪个版本要根据具体的需求和项目情况来决定。详细介绍:1、性能提升,PHP8引入了Just-in-Time(JIT)编译器,可以提高代码的执行速度;2、新特性和语法改进,PHP8支持命名参数和可选参数的声明,使得函数调用更加灵活;引入了匿名类、属性的类型声明等等。

PHP服务器环境常见的解决方法包括:确保已安装正确的PHP版本和已复制相关文件到模块目录。临时或永久禁用SELinux。检查并配置PHP.ini,确保已添加必要的扩展和进行正确设置。启动或重启PHP-FPM服务。检查DNS设置是否存在解析问题。

php7.0安装部署的方法:1、到PHP官网下载与本机系统对应的安装版本;2、将下载的zip文件解压到指定目录;3、打开命令行窗口,在“E:\php7”目录下运行“php -v”命令即可。

随着互联网技术的发展,计算机编程语言也随之不断发展和更新。PHP作为一种广泛应用于Web开发领域的编程语言,在多年的发展中经历了多个版本的更新,而最新版的PHP7又在性能和稳定性上有了巨大提升。为了能更好地应用PHP编程语言,这篇文章将介绍PHP7的下载和安装教程,供初学者参考。

php7和php5语法区别有:1、PHP7引入了严格的类型声明,而PHP5变量的类型是隐式的;2、PHP7引入了对标量类型声明的支持,而PHP5并没有;3、PHP7引入了NULL合并运算符,而PHP5检查一个变量是否存在并且不为null,需要使用条件语句;4、PHP7添加了新的比较运算符“<=>”,而PHP5并没有;5、PHP7引入新特性匿名类,而PHP5并没有。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version
Chinese version, very easy to use

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver Mac version
Visual web development tools
