PHP unset浅谈

一 定义

unset() 函数用于销毁给定的变量。

起初我以为销毁了变量,那内存肯定就释放了,实际上并不是如此。

二 unset整个数组

<?php 
$arr = [];
echo(date('Y-m-d H:i:s').'|'.__LINE__ . '| Current memory usage: ' . (memory_get_usage(true) / 1024 / 1024) . " MB" );
echo "<br/>";
for ($i=0; $i < 1000000; $i++) { 
    $arr[$i] = $i;
}
echo(date('Y-m-d H:i:s').'|'.__LINE__ . '| Current memory usage: ' . (memory_get_usage(true) / 1024 / 1024) . " MB" );
echo "<br/>";
unset($arr);
echo(date('Y-m-d H:i:s').'|'.__LINE__ . '| Current memory usage: ' . (memory_get_usage(true) / 1024 / 1024) . " MB" );
echo "<br/>";
2023-02-21 12:00:09|3| Current memory usage: 2 MB
2023-02-21 12:00:09|8| Current memory usage: 34.00390625 MB
2023-02-21 12:00:09|11| Current memory usage: 2 MB

二 unset数组成员

<?php 
$arr = [];
echo(date('Y-m-d H:i:s').'|'.__LINE__ . '| Current memory usage: ' . (memory_get_usage(true) / 1024 / 1024) . " MB" );
echo "<br/>";
for ($i=0; $i < 1000000; $i++) { 
    $arr[$i] = $i;
}
echo(date('Y-m-d H:i:s').'|'.__LINE__ . '| Current memory usage: ' . (memory_get_usage(true) / 1024 / 1024) . " MB" );
echo "<br/>";
/*unset($arr);
echo(date('Y-m-d H:i:s').'|'.__LINE__ . '| Current memory usage: ' . (memory_get_usage(true) / 1024 / 1024) . " MB" );
echo "<br/>";*/
echo count($arr);
echo "<br/>";
for ($i=0; $i < 1000000; $i++) { 
    unset($arr[$i]);
}
echo(date('Y-m-d H:i:s').'|'.__LINE__ . '| Current memory usage: ' . (memory_get_usage(true) / 1024 / 1024) . " MB" );
echo "<br/>";
echo count($arr);
echo "<br/>";


for ($i=1000000; $i < 2000000; $i++) { 
    $arr[$i] = $i;
}
echo(date('Y-m-d H:i:s').'|'.__LINE__ . '| Current memory usage: ' . (memory_get_usage(true) / 1024 / 1024) . " MB" );
echo "<br/>";
echo count($arr);
echo "<br/>";
2023-02-21 12:01:45|3| Current memory usage: 2 MB
2023-02-21 12:01:45|8| Current memory usage: 34.00390625 MB
1000000
2023-02-21 12:01:45|18| Current memory usage: 34.00390625 MB
0
2023-02-21 12:01:45|27| Current memory usage: 38 MB
1000000

 

三 unset数组成员后,新开一个数组

<?php 
$arr = [];
echo(date('Y-m-d H:i:s').'|'.__LINE__ . '| Current memory usage: ' . (memory_get_usage(true) / 1024 / 1024) . " MB" );
echo "<br/>";
for ($i=0; $i < 1000000; $i++) { 
    $arr[$i] = $i;
}
echo(date('Y-m-d H:i:s').'|'.__LINE__ . '| Current memory usage: ' . (memory_get_usage(true) / 1024 / 1024) . " MB" );
echo "<br/>";
/*unset($arr);
echo(date('Y-m-d H:i:s').'|'.__LINE__ . '| Current memory usage: ' . (memory_get_usage(true) / 1024 / 1024) . " MB" );
echo "<br/>";*/
echo count($arr);
echo "<br/>";
for ($i=0; $i < 1000000; $i++) { 
    unset($arr[$i]);
}
echo(date('Y-m-d H:i:s').'|'.__LINE__ . '| Current memory usage: ' . (memory_get_usage(true) / 1024 / 1024) . " MB" );
echo "<br/>";
echo count($arr);
echo "<br/>";


for ($i=1000000; $i < 2000000; $i++) { 
    $arr[$i] = $i;
}
echo(date('Y-m-d H:i:s').'|'.__LINE__ . '| Current memory usage: ' . (memory_get_usage(true) / 1024 / 1024) . " MB" );
echo "<br/>";
echo count($arr);
echo "<br/>";

$arr2 = [];
for ($i=0; $i < 1000000; $i++) {
    $arr2[$i] = $i;
}
echo(date('Y-m-d H:i:s').'|'.__LINE__ . '| Current memory usage: ' . (memory_get_usage(true) / 1024 / 1024) . " MB" );
echo "<br/>";
2023-02-21 12:02:53|3| Current memory usage: 2 MB
2023-02-21 12:02:53|8| Current memory usage: 34.00390625 MB
1000000
2023-02-21 12:02:53|18| Current memory usage: 34.00390625 MB
0
2023-02-21 12:02:53|27| Current memory usage: 38 MB
1000000
2023-02-21 12:02:53|36| Current memory usage: 70.00390625 MB

 

四 总结

  • unset整个arr,会立即释放内存
  • unset arr的kv, 内存暂时不变。但是如果在本arr中插入新的kv,他会复用unset的内存
  • unset arr所有成员之后,新建一个arr2,会新开内存,此时系统占用内存为arr释放前➕arr2新开的内存和

 

posted @ 2023-02-21 12:06  布叔喂丶  阅读(45)  评论(0编辑  收藏  举报