当前位置:网站首页>PHP memory management mechanism and garbage collection mechanism
PHP memory management mechanism and garbage collection mechanism
2022-07-25 18:10:00 【My sweet】
PHP Memory management mechanism
1 var_dump(memory_get_usage()); // Get memory
2 $a = "laruence"; // Define a variable
3 var_dump(memory_get_usage()); // Get memory after defining variables
4 unset($a); // Delete the variable
5 var_dump(memory_get_usage()); // Get memory after deleting variables
As can be seen from the above php The memory management mechanism of is : Give a space in advance , Used to store variables , When there is not enough space , Apply for a new space .
1. Store variable names , There is a symbol table .
2. Variable values are stored in memory space .
3. When deleting variables , Will free up the space for variable value storage , The symbol table of variable names will not be reduced .
var_dump(memory_get_usage()); // Get memory
// Definition 100 A variable
for($i=0;$i<100;$i++)
{
$a = "test".$i;
$$a = "hello";
}
// Get definition 100 Memory after variables
var_dump(memory_get_usage());
// Definition 100 Variables and delete
for($i=0;$i<100;$i++)
{
$a = "test".$i;
unset($$a);
}
// Get the memory after deletion
var_dump(memory_get_usage());
As can be seen from the above , Although the memory becomes smaller after deletion , But it is still larger than before the variable was defined , This is because although the value of the variable is deleted , But the variable name was not deleted .
php Garbage collection mechanism
PHP Variable storage is stored in a zval containerized
1. type 2. value 3.is_ref Represents whether there is an address reference 4.refcount The number of variables that point to this value
1. When you assign a variable :is_ref by false refcount by 1
$a = 1;
xdebug_debug_zval('a');
echo PHP_EOL;
2. Put the variable a The value of is assigned to the variable b, Variable b Will not immediately store values in memory , Instead, point to the variable first a Value , All the way to variables a When there is any operation
$b = $a;
xdebug_debug_zval('a');
echo PHP_EOL;
3. Because the program operates variables again a, So the variable b Will apply for a piece of memory to put the value in . So the variable a Of zavl In the container refcount Will be reduced 1 Turn into 1, Variable c Point to a, therefore refcount Will add 1 Turn into 2
$c = &$a;
xdebug_debug_zval('a');
echo PHP_EOL;
xdebug_debug_zval('b');
echo PHP_EOL;
Garbage collection :
1. stay 5.2 Version or earlier ,PHP Will be based on refcount Value to judge whether it is garbage
If refcount The value is 0,PHP It will be released as garbage
This recycling mechanism is flawed , Variables with ring references cannot be recycled
2. stay 5.3 Later versions improved the garbage collection mechanism
If you find a zval In container refcount On the increase , It's not rubbish
If you find a zval In container refcount It's reducing , If it's down to 0, Directly as garbage collection If you find a zval In container refcount It's reducing , Not reduced to 0,PHP Will put the value in the buffer , As a suspect who might be garbage . When the buffer reaches a critical value ,PHP It will automatically call a method to traverse each value , If you find it is rubbish, clean it up
边栏推荐
- php内存管理机制与垃圾回收机制
- Hit the test site directly: summary of common agile knowledge points in PMP examination
- Recognized by international authorities! Oceanbase was selected into the Forrester translational data platform report
- 绘制pdf表格 (二) 通过itext实现在pdf中绘制excel表格样式设置中文字体、水印、logo、页眉、页码
- Mongodb cluster and sharding
- TME2022校园招聘后台开发/运营开发/业务运维/应用开发笔试(I)编程题的一点自我分析
- STM8S003F3 内部flash调试
- SDLC software development life cycle and model
- Nineteen year old summary
- 数二2010真题考点
猜你喜欢

大话DevOps监控,团队如何选择监控工具?

What scenarios have rust, which is becoming more and more mature, applied?

数二2010真题考点

Nineteen year old summary

为什么数字化未来取决于3D实时渲染

SVN客户端(TortoiseSVN)安装及使用说明

使用sqldeveloper连接mysql

LeetCode 101. 对称二叉树 && 100. 相同的树 && 572. 另一棵树的子树

Oracle使用impdp导入报错:ORA-39001: 参数值无效 ORA-39000: 转储文件说明错误 ORA-39088: 文件名不能包含路径说明

"Digital security" alert NFT's seven Scams
随机推荐
Keil5 "loading PDSC debug description failed for STMicroelectronics stm32hxxxxxxx" solution
imx6 RTL8189FTV移植
绘制pdf表格 (一) 通过itext实现在pdf中绘制excel表格样式并且实现下载(支持中文字体)
Mock服务moco系列(一)- 简介、第一个Demo、Get请求、Post请求
CVE-2022-33891 Apache spark shell 命令注入漏洞复现
CH582 BLE 5.0 使用 LE Coded 广播和连接
STM32F105RBT6 内部flash调试
UFT(QTP)-总结点与自动化测试框架
Postman get started quickly
Good news! Ruiyun technology was awarded the member unit of 5g integrated application special committee of "sailing on the sea"
MySQL 索引优化全攻略
C语言 cJSON库的使用
Stm8s003f3 internal flash debugging
如何判断静态代码质量分析工具的性能?这五大因素必须考虑
Mock service Moco series (III) - redirection, regular expression, delay, template, event, sub module design
Drawing PDF tables (I) drawing excel table styles in PDF and downloading them through iText (supporting Chinese fonts)
TME2022校园招聘后台开发/运营开发/业务运维/应用开发笔试(I)编程题的一点自我分析
Recommend a qinheng Bluetooth reference blog
Brief introduction of bubble sort and quick sort
Several implementations of PHP to solve concurrency problems