当前位置:网站首页>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
边栏推荐
- Drawing PDF tables (I) drawing excel table styles in PDF and downloading them through iText (supporting Chinese fonts)
- Construction of Huffman tree
- CH582 BLE 5.0 使用 LE Coded 广播和连接
- Itextpdf realizes the merging of multiple PDF files into one PDF document
- nodejs 简单例子程序之express
- 越来越成熟的Rust,都应用了哪些场景呢?
- BL602 开发环境搭建
- MySQL page lock
- What is an IP SSL certificate and how to apply for it?
- CVE-2022-33891 Apache spark shell 命令注入漏洞复现
猜你喜欢

专访即构科技李凯:音视频的有趣、行业前沿一直吸引着我

CVE-2022-33891 Apache spark shell 命令注入漏洞复现

「行话」| 用DevOps高效交付游戏,是种什么体验?
![Why is the index in [mysql] database implemented by b+ tree? Is hash table / red black tree /b tree feasible?](/img/1f/a2d50ec6bc97d52c1e7566a42e564b.png)
Why is the index in [mysql] database implemented by b+ tree? Is hash table / red black tree /b tree feasible?

数二2010真题考点

Installation and operation instructions of SVN client (TortoiseSVN)

Drawing PDF tables (I) drawing excel table styles in PDF and downloading them through iText (supporting Chinese fonts)

CH582 BLE 5.0 使用 LE Coded 广播和连接

Imx6 rtl8189ftv migration

越来越成熟的Rust,都应用了哪些场景呢?
随机推荐
What is the relationship between cloud fluidization and cloud desktop
Mock service Moco series (I) - introduction, first demo, get request, post request
Oracle导入出错:IMP-00038: 无法转换为环境字符集句柄
大话DevOps监控,团队如何选择监控工具?
Mock服务moco系列(二)- Json格式、File文件、Header、Cookie、解决中文乱码
「行话」| 用DevOps高效交付游戏,是种什么体验?
Stm8s003f3 internal flash debugging
Use of C language cjson Library
UnitTest框架应用
SDLC 软件开发生命周期及模型
「数字安全」警惕 NFT的七大骗局
TESTNG中的并发测试invocationCount, threadPoolSize, timeOut的使用
PageHelper can also be combined with lambda expressions to achieve concise paging encapsulation
What are the advantages of real-time cloud rendering
imx6 RTL8189FTV移植
Postman快速上手
UFT (QTP) - summary points and automated test framework
越来越成熟的Rust,都应用了哪些场景呢?
CH582 BLE 5.0 使用 LE Coded 广播和连接
Stm32f105rbt6 internal flash debugging