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.MVC的模板标签系统(二)
Sep 05 PHP
PHP 一个随机字符串生成代码
May 26 PHP
php基于str_pad实现卡号不足位数自动补0的方法
Nov 12 PHP
PHP中is_dir()函数使用指南
May 08 PHP
关于php微信订阅号开发之token验证后自动发送消息给订阅号但是没有消息返回的问题
Dec 21 PHP
php基于dom实现读取图书xml格式数据的方法
Feb 03 PHP
一键生成各种尺寸Icon的php脚本(实例)
Feb 08 PHP
Yii1.1中通过Sql查询进行的分页操作方法
Mar 16 PHP
PHP CodeIgniter分页实例及多条件查询解决方案(推荐)
May 20 PHP
php在windows环境下获得cpu内存实时使用率(推荐)
Feb 08 PHP
ThinkPHP like模糊查询,like多匹配查询,between查询,in查询,一般查询书写方法
Sep 26 PHP
Laravel框架控制器的middleware中间件用法分析
Sep 30 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 文件上传实例代码
2012/04/19 PHP
php日历制作代码分享
2014/01/20 PHP
php时间函数用法分析
2016/05/28 PHP
php session的应用详细介绍
2017/03/22 PHP
jQuery学习笔记之jQuery的DOM操作
2010/12/22 Javascript
js事件(Event)知识整理
2012/10/11 Javascript
js实现在文本框光标处添加字符的方法介绍
2012/11/24 Javascript
javascript 回到顶部效果的实现代码
2014/02/17 Javascript
js实现从数组里随机获取元素
2015/01/12 Javascript
JS实现统计复选框选中个数并提示确定与取消的方法
2015/07/01 Javascript
js实现楼层效果的简单实例
2016/07/15 Javascript
详解js的异步编程技术的方法
2017/02/09 Javascript
JavaScript对象的浅拷贝与深拷贝实例分析
2018/07/25 Javascript
jQuery设置下拉框显示与隐藏效果的方法分析
2019/09/15 jQuery
Vue.js组件通信之自定义事件详解
2019/10/19 Javascript
[48:54]VGJ.T vs infamous Supermajor小组赛D组败者组第一轮 BO3 第二场 6.3
2018/06/04 DOTA
python判断端口是否打开的实现代码
2013/02/10 Python
Linux下用Python脚本监控目录变化代码分享
2015/05/21 Python
python读取txt文件中特定位置字符的方法
2018/12/24 Python
Python 20行简单实现有道在线翻译的详解
2019/05/15 Python
python如何实现代码检查
2019/06/28 Python
Python semaphore evevt生产者消费者模型原理解析
2020/03/18 Python
tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this T
2020/06/22 Python
床上用品全球在线购物:BeddingInn
2016/12/18 全球购物
J2EE面试题大全
2016/08/06 面试题
Overload和Override的区别。Overloaded的方法是否可以改变返回值的类型
2013/10/30 面试题
网络工程师个人的自我评价范文
2013/10/01 职场文书
节能宣传周活动总结
2014/05/08 职场文书
法英专业大学生职业生涯规划范文:衡外情,量己力!
2014/09/23 职场文书
财务工作失职检讨书
2014/11/21 职场文书
关于倡议书的范文
2015/04/29 职场文书
介绍信怎么写
2015/05/05 职场文书
2015法院个人工作总结范文
2015/05/25 职场文书
运动会主持人开幕词
2016/03/04 职场文书
李清照的诗词赏析(20首)
2019/08/22 职场文书
python 破解加密zip文件的密码
2021/04/22 Python