当前位置:网站首页>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
边栏推荐
- Mock service Moco series (II) - JSON format, file file, header, cookie, solving Chinese garbled code
- 排序还需要了解的信息以及链表
- Mongodb cluster and sharding
- MySQL page lock
- Unity 贝塞尔曲线的创建
- OV7725 yuv 640*[email protected] 配置文件
- PageHelper还能结合Lambda表达式实现简洁的分页封装
- Keil5 “Loading PDSC Debug Description Failed for STMicroelectronics STM32Hxxxxxxx”解决办法
- 「数字安全」警惕 NFT的七大骗局
- Cloud XR面临的问题以及Cloud XR主要应用场景
猜你喜欢

Auditing related notes

Problems faced by cloud XR and main application scenarios of cloud XR

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

STM8S003F3 内部flash调试
SQL optimizer parsing | youth training camp notes

What is an IP SSL certificate and how to apply for it?

「数字安全」警惕 NFT的七大骗局

SLA 、SLO & SLI

Idea integrates common functions of SVN code management

如何选择数字孪生可视化平台
随机推荐
云VR:虚拟现实专业化的下一步
Conversion between integer and string in C language
Good news! Ruiyun technology was awarded the member unit of 5g integrated application special committee of "sailing on the sea"
mysql的小数number类型select之后丢失了前面的0
云流化和云桌面有什么关系
Redis source code and design analysis -- 17. Redis event processing
C语言 cJSON库的使用
How to judge the performance of static code quality analysis tools? These five factors must be considered
CH582 BLE 5.0 使用 LE Coded 广播和连接
Mock服务moco系列(一)- 简介、第一个Demo、Get请求、Post请求
408第二章线性表
Briefly describe synchronized and lock upgrade
Tme2022 campus recruitment background development / operation development / business operation and maintenance / application development written examination (I) a little self analysis of programming q
Auditing相关注解
关于云XR介绍,以及5G时代云化XR的发展机遇
二叉树的相关操作
「数字安全」警惕 NFT的七大骗局
H5 test point (mind map)
TESTNG中的并发测试invocationCount, threadPoolSize, timeOut的使用
MySQL page lock