PHP中使用unset销毁变量并内存释放问题


Posted in PHP onJuly 05, 2012
for ( $i = 1; $i < 100; $i++ ) { 
$str = str_repeat('01234567', $i); 
$a = memory_get_usage(); 
unset($str); 
$b = memory_get_usage(); 
echo "\n 
".$i.': '.($b - $a).' Bytes.'; 
}

从结果看出:
8 x 32 = 256 在256字节长的时候才真正有必要释放内存,有些人说,不如直接$str = null来的速度快。
结果如下:
1: 0 Bytes.
2: 0 Bytes.
3: 0 Bytes.
4: 0 Bytes.
5: 0 Bytes.
6: 0 Bytes.
7: 0 Bytes.
8: 0 Bytes.
9: 0 Bytes.
10: 0 Bytes.
11: 0 Bytes.
12: 0 Bytes.
13: 0 Bytes.
14: 0 Bytes.
15: 0 Bytes.
16: 0 Bytes.
17: 0 Bytes.
18: 0 Bytes.
19: 0 Bytes.
20: 0 Bytes.
21: 0 Bytes.
22: 0 Bytes.
23: 0 Bytes.
24: 0 Bytes.
25: 0 Bytes.
26: 0 Bytes.
27: 0 Bytes.
28: 0 Bytes.
29: 0 Bytes.
30: 0 Bytes.
31: 0 Bytes.
32: -272 Bytes.
33: -280 Bytes.
34: -288 Bytes.
35: -296 Bytes.
36: -304 Bytes.
37: -312 Bytes.
38: -320 Bytes.
39: -328 Bytes.
40: -336 Bytes.
41: -344 Bytes.
42: -352 Bytes.
43: -360 Bytes.
44: -368 Bytes.
45: -376 Bytes.
46: -384 Bytes.
47: -392 Bytes.
48: -400 Bytes.
49: -408 Bytes.
50: -416 Bytes.
51: -424 Bytes.
52: -432 Bytes.
53: -440 Bytes.
54: -448 Bytes.
55: -456 Bytes.
56: -464 Bytes.
57: -472 Bytes.
58: -480 Bytes.
59: -488 Bytes.
60: -496 Bytes.
61: -504 Bytes.
62: -512 Bytes.
63: -520 Bytes.
64: -528 Bytes.
65: -536 Bytes.
66: -544 Bytes.
67: -552 Bytes.
68: -560 Bytes.
69: -568 Bytes.
70: -576 Bytes.
71: -584 Bytes.
72: -592 Bytes.
73: -600 Bytes.
74: -608 Bytes.
75: -616 Bytes.
76: -624 Bytes.
77: -632 Bytes.
78: -640 Bytes.
79: -648 Bytes.
80: -656 Bytes.
81: -664 Bytes.
82: -672 Bytes.
83: -680 Bytes.
84: -688 Bytes.
85: -696 Bytes.
86: -704 Bytes.
87: -712 Bytes.
88: -720 Bytes.
89: -728 Bytes.
90: -736 Bytes.
91: -744 Bytes.
92: -752 Bytes.
93: -760 Bytes.
94: -768 Bytes.
95: -776 Bytes.
96: -784 Bytes.
97: -792 Bytes.
98: -800 Bytes.
99: -808 Bytes.

我们先看一个例子

<?php 
$s=str_repeat('1',255); //产生由255个1组成的字符串 
$m=memory_get_usage(); //获取当前占用内存 
unset($s); 
$mm=memory_get_usage(); //unset()后再查看当前占用内存 
echo $m-$mm; 
?>

最后输出unset()之前占用内存减去unset()之后占用内存,如果是正数,那么说明unset($s)已经将$s从内存中销毁(或者说,unset()之后内存占用减少了),可是我在PHP5和windows平台下,得到的结果是:-48。这是否可以说明,unset($s)并没有起到销毁变量$s所占用内存的作用呢?我们再作下面的例子:
<?php 
$s=str_repeat('1',256); //产生由256个1组成的字符串 
$m=memory_get_usage(); //获取当前占用内存 
unset($s); 
$mm=memory_get_usage(); //unset()后再查看当前占用内存 
echo $m-$mm; 
?>

这个例子,和上面的例子几乎相同,唯一的不同是,$s由256个1组成,即比第一个例子多了一个1,得到结果是:224。这是否可以说明,unset($s)已经将$s所占用的内存销毁了?
通过上面两个例子,我们可以得出以下结论:结论一、unset()函数只能在变量值占用内存空间超过256字节时才会释放内存空间。
那么是不是只要变量值超过256,使用unset就可以释放内存空间呢?我们再通过一个例子来测试一下:
<?php 
$s=str_repeat('1',256); //这和第二个例子完全相同 
$p=&$s; 
$m=memory_get_usage(); 
unset($s); //销毁$s 
$mm=memory_get_usage(); 
echo $p.'<br />'; 
echo $m-$mm; 
?>

'刷新页面,我们看到第一行有256个1,第二行是-48,按理说我们已经销毁了$s,而$p只是引用$s的变量,应该是没有内容了,另外,unset($s)后内存占用却比unset()前增加了!现在我们再做以下的例子:
<?php 
$s=str_repeat('1',256); //这和第二个例子完全相同 
$p=&$s; 
$m=memory_get_usage(); 
$s=null; //设置$s为null 
$mm=memory_get_usage(); 
echo $p.'<br />'; 
echo $m-$mm; 
?>

现在刷新页面,我们看到,输出$p已经是没有内容了,unset()前后内存占用量之差是224,即已经清除了变量占用的内存。本例中的$s=null也可以换成unset(),如下:
<?php 
$s=str_repeat('1',256); //这和第二个例子完全相同 
$p=&$s; 
$m=memory_get_usage(); 
unset($s); //销毁$s 
unset($p); 
$mm=memory_get_usage(); 
echo $p.'<br />'; 
echo $m-$mm; 
?>

我们将$s和$p都使用unset()销毁,这时再看内存占用量之差也是224,说明这样也可以释放内存。那么,我们可以得到另外一条结论:结论二、只有当指向该变量的所有变量(如引用变量)都被销毁后,才会释放内存。
相信经过本文的例子后,大家应该对unset()有所了解了,最起码,本人用unset()也是为了在变量不起作用时,释放内存。
PHP 相关文章推荐
社区(php&amp;&amp;mysql)五
Oct 09 PHP
用session做客户验证时的注意事项
Oct 09 PHP
在数据量大(超过10万)的情况下
Jan 15 PHP
PHP 函数语法介绍一
Jun 14 PHP
ThinkPHP3.1新特性之查询条件预处理简介
Jun 19 PHP
php中json_encode UTF-8中文乱码的更好解决方法
Sep 28 PHP
php限制ip地址范围的方法
Mar 31 PHP
PHP中iconv函数知识汇总
Jul 02 PHP
PHP自带方法验证邮箱是否存在
Feb 01 PHP
Yii2中cookie用法示例分析
Jul 18 PHP
django中的ajax组件教程详解
Oct 18 PHP
PHP 文件上传限制问题
Sep 01 PHP
php Ubb代码编辑器函数代码
Jul 05 #PHP
PHP取整数函数常用的四种方法小结
Jul 05 #PHP
PHP函数学习之PHP函数点评
Jul 05 #PHP
php中根据变量的类型 选择echo或dump
Jul 05 #PHP
PHP写的求多项式导数的函数代码
Jul 04 #PHP
php中禁止单个IP与ip段访问的代码小结
Jul 04 #PHP
提高php运行速度的一些小技巧分享
Jul 03 #PHP
You might like
php日历[测试通过]
2008/03/27 PHP
php&amp;mysql 日期操作小记
2012/02/27 PHP
将二维数组转为一维数组的2种方法
2014/05/26 PHP
PHP实现获取图片颜色值的方法
2014/07/11 PHP
PHP针对JSON操作实例分析
2015/01/12 PHP
PHP调用Linux命令权限不足问题解决方法
2015/02/07 PHP
PHP中实现crontab代码分享
2015/03/26 PHP
php强大的时间转换函数strtotime
2016/02/18 PHP
php原生数据库分页的代码实例
2019/02/18 PHP
Jquery中删除元素的实现代码
2011/12/29 Javascript
基于jquery实现简单的分页控件
2016/03/17 Javascript
jQuery ajax调用后台aspx后台文件的两种常见方法(不是ashx)
2016/06/28 Javascript
vuejs父子组件通信的问题
2017/01/11 Javascript
微信小程序 动态传参实例详解
2017/04/27 Javascript
JS Testing Properties 判断属性是否在对象里的方法
2017/10/01 Javascript
JavaScript多线程运行库Nexus.js详解
2017/12/22 Javascript
Vuex提升学习篇
2018/01/11 Javascript
详解webpack-dev-server使用方法
2018/09/14 Javascript
puppeteer实现html截图的示例代码
2019/01/10 Javascript
node中使用es6/7/8(支持性与性能)
2019/03/28 Javascript
python批量提取word内信息
2015/08/09 Python
Python退火算法在高次方程的应用
2018/07/26 Python
python对矩阵进行转置的2种处理方法
2019/07/17 Python
python自带tkinter库实现棋盘覆盖图形界面
2019/07/17 Python
浅谈pytorch、cuda、python的版本对齐问题
2020/01/15 Python
Python模块 _winreg操作注册表
2020/02/05 Python
使用IPython或Spyder将省略号表示的内容完整输出
2020/04/20 Python
基于jupyter代码无法在pycharm中运行的解决方法
2020/04/21 Python
selenium学习教程之定位以及切换frame(iframe)
2021/01/04 Python
html5适合移动应用开发的12大特性
2014/03/19 HTML / CSS
高中生校园生活自我评价
2013/09/19 职场文书
气象学专业个人求职信
2014/03/15 职场文书
个人优缺点总结
2015/02/28 职场文书
学生党支部工作总结2015
2015/05/26 职场文书
2016年小学生寒假总结
2015/10/10 职场文书
晶体管来复再生式二管收音机
2021/04/22 无线电