当前位置:网站首页>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
边栏推荐
- H5测试点(思维导图)
- "Deprecated gradle features were used in this build, making it incompatible with gradle 6.0" problem solving
- Could not stop Cortex-M device! please check the JTAG cable的解决办法
- 简述冒泡排序与快速排序
- Type assertion of go interface variables
- 期货开户哪家最好最安全
- BiSeNet v1
- SDLC software development life cycle and model
- Linux启动mysql报错
- CVE-2022-33891 Apache spark shell 命令注入漏洞复现
猜你喜欢

云VR:虚拟现实专业化的下一步

Unity 贝塞尔曲线的创建

Keil5 "loading PDSC debug description failed for STMicroelectronics stm32hxxxxxxx" solution

Drawing PDF form (II) drawing excel form style in PDF through iText, setting Chinese font, watermark, logo, header and page number

Talking about Devops monitoring, how does the team choose monitoring tools?
![[MySQL]数据库中的索引为什么是用B+树来实现? 哈希表/红黑树/B树是否可行呢?](/img/1f/a2d50ec6bc97d52c1e7566a42e564b.png)
[MySQL]数据库中的索引为什么是用B+树来实现? 哈希表/红黑树/B树是否可行呢?

2022/7/23

SLA 、SLO & SLI

ORB_SLAM3复现——上篇

BiSeNet v1
随机推荐
Postman get started quickly
Redis source code and design analysis -- 15. RDB persistence mechanism
Postman快速上手
为什么数字化未来取决于3D实时渲染
testng执行顺序的3中控制方法
绘制pdf表格 (二) 通过itext实现在pdf中绘制excel表格样式设置中文字体、水印、logo、页眉、页码
简述聚簇索引、二级索引、索引下推
What are the advantages of real-time cloud rendering
MySQL数据库常用命令
Drawing PDF tables (I) drawing excel table styles in PDF and downloading them through iText (supporting Chinese fonts)
软件测试基础知识(思维导图)
tkinter GUI版通信录管理系统
Which real-time gold trading platform is reliable and safe?
MATLAB中join函数使用
Imx6 rtl8189ftv migration
List转换问题
喜讯!瑞云科技被授予“海上扬帆”5G融合应用专委会成员单位
Bl602 development environment setup
Creation of unity Bezier curve
2022/7/23