PHP内存溢出 Allowed memory size of 解决办法
PHP出现如下错误:Allowed memory size of xxx bytes exhausted at xxx:xxx (tried to allocate xxx bytes)
关于这一点,本站点中,http://nodonkey.iteye.com/blog/728223 有所讲述。
同时,还有 http://hi.baidu.com/thinkinginlamp/blog/item/e400f819a3caab7cdbb4bd4e.html 此文也是讲这个问题的。
为什么大家都讲了,这里还要讲,是不是多此一举?其实不是。这个问题的英文原文是在:http://paul-m-jones.com/archives/262
可以看出,有必要再次总结一下。
如果PHP对象存在递归引用,就会出现内存泄漏。这个Bug在PHP里已经存在很久很久了,先让我们来重现这个Bug:
- <?php
- class Foo {
- function __construct() {
- $this->bar = new Bar($this);
- }
- }
- class Bar {
- function __construct($foo) {
- $this->foo = $foo;
- }
- }
- for ($i = 0; $i < 100; $i++) {
- $obj = new Foo();
- unset($obj);
- echo memory_get_usage(), "\n";
- }
- ?>
运行以上代码,你会发现,内存使用量本应该不变才对,可实际上却是不断增加,unset没有完全生效。
我们看到,英文原文中给出的做法是,手工调用析构函数。为什么要这样做呢?
我们给出一段新的代码,注意一下,代码中的注释对问题的讲解:
- <?php
- class Foo {
- function __construct() {
- //PHP5.3以前的版本,它是一个长生不老药。因为创建了它,引用了自己。
- //实际上,只要外部有任一个对象的属性中引用了这个类,这个类和引用它的类都不能被unset
- //这就是我们在这里犯下了错。
- $this->bar = new Bar($this);
- }
- function __destruct() {
- echo('Foo die --3--<br>');
- unset($this->bar);
- }
- /**
- * 因为我们在这里犯下了错,我们要在此收拾残局。所以,添加一个收尸函数
- * 这一做法,也就是不要在外面调用__destruct,看上去很不雅观。
- * 但增加此函数有一大好处。就是向使用者表明,需要我们主动把它杀掉。而不是等它自动死掉。
- * 如果不愿意,你大可以直接在外部调用__destruct
- */
- function destroy()
- {
- echo('destroy --1--<br>');
- unset($this->bar);
- }
- }
- class Bar {
- function __construct($foo) {
- $this->foo = $foo;
- }
- function __destruct() {
- echo('Bar die --2--<br>');
- }
- }
- for ($i = 0; $i < 100; $i++) {
- echo memory_get_usage(), "<br>";
- $obj = new Foo();
- //注解掉,或放开注解下面这一行,运行一下。结果分别在本文后面。
- //我们可以发现,如果我们不收尸,那么,PHP主动调用__destruct,则是在程序结束时才执行。
- //如果我们主动做了,就能及时释放内存。
- $obj->destroy();
- unset($obj);
- }
- echo memory_get_usage(), "<br>";
- ?>
我们可以发现,当我们注解掉上面这段代码中的42行[$obj->destroy();]时,__destruct中的调用被PHP延迟了。即如果PHP对象存在递归引用,PHP并不及时释放内存。即使你使用unset,希望强制释放也没有用。
我们引用部分运行结果:
99616
99984
100352
100720
101088
Foo die --3--
Bar die --2--
Foo die --3--
Bar die --2--
Foo die --3--
Bar die --2--
Foo die --3--
Bar die --2--
Foo die --3--
Bar die --2--
可以看出Foo,Bar是在程序结束时死的,并且,Foo先死,Bar后死。这是因为,PHP明了,你们都不需要它们了。
一旦我们手工调用[$obj->destroy();],由于PHP对象存在递归引用已被破坏,所以,unset就起作用了。
我们引用部分运行结果:
64256
destroy --1--
Bar die --2--
Foo die --3--
65008
destroy --1--
Bar die --2--
Foo die --3--
65008
destroy --1--
Bar die --2--
Foo die --3--
可以看出,Foo,Bar是在你调用destroy后立即死亡,内存因而不再增长了。
此BUG据说是在php5.3中解决了。如果说是在php5.3中可以手工调用gc_collect_cycles这个函数。
假如是这样,其实与你在class中手工添加destroy让用户调用,没有什么区别。
递归引用会被经常用到。特别是用PHP构建树结构对象。注意英文原文中,用户评论中的链接:http://www.alexatnet.com/node/73
其中给出的代码:
- class Node {
- public $parentNode;
- public $childNodes = array();
- function Node() {
- $this->nodeValue = str_repeat('0123456789', 128);
- }
- }
- function createRelationship() {
- $parent = new Node();
- $child = new Node();
- $parent->childNodes[] = $child;
- $child->parentNode = $parent;
- }
- //And then let's review the amount of memory allocated by this script after 10,000 calls to createRelationship function.
- echo 'Initial: ' . number_format(memory_get_usage(), 0, '.', ',') . " bytes\n";
- for($i = 0; $i < 10000; $i++) {
- createRelationship();
- }
- echo 'Peak: ' . number_format(memory_get_peak_usage(), 0, '.', ',') . " bytes\n";
- echo 'End: ' . number_format(memory_get_usage(), 0, '.', ',') . " bytes\n";
输出结果是:
Initial: 327,336 bytes
Peak: 35,824,176 bytes
End: 35,823,328 bytes
作者建议的方式就是:增加destroy方法,
- class Node {
- public $parentNode;
- public $childNodes = array();
- function Node() {
- $this->nodeValue = str_repeat('0123456789', 128);
- }
- function destroy()
- {
- $this->parentNode = null;
- $this->childNodes = array();
- }
- }
- function createRelationship() {
- $parent = new Node();
- $child = new Node();
- $parent->childNodes[] = $child;
- $child->parentNode = $parent;
- $parent->destroy();
- }
另外作者还给出了php5.3中的解决方法:使用php5.3中的新函数,手工调用gc_collect_cycles这个函数。
- function createRelationship() {
- $parent = new Node();
- $child = new Node();
- $parent->childNodes[] = $child;
- $child->parentNode = $parent;
- gc_collect_cycles();
- }
同时,作者也告诉了我们,调用gc_collect_cycles这个函数,我们不需要额外增加destroy了,但是性能上有一些缺陷。
我们还可以对代码作进一步改进。那就是改成静态函数。具体如下:
- <?php
- class Foo {
- function __construct() {
- //PHP5.3以前的版本,它是一个长生不老药。因为创建了它,引用了自己。
- //实际上,只要外部有任一个对象的属性中引用了这个类,这个类和引用它的类都不能被unset
- //这就是我们在这里犯下了错。
- $this->bar = new Bar($this);
- }
- function __destruct() {
- echo('Foo die --3--<br>');
- unset($this->bar);
- }
- /**
- * 因为我们在这里犯下了错,我们要在此收拾残局。所以,添加一个收尸函数
- * 这一做法,也就是不要在外面调用__destruct,看上去很不雅观。
- * 但增加此函数有一大好处。就是向使用者表明,需要我们主动把它杀掉。而不是等它自动死掉。
- * 如果不愿意,你大可以直接在外部调用__destruct
- * 实际上,这样做以后,也就不用清理内存时,写两行代码,一行代码就够了。
- */
- static function destroy($obj)
- {
- echo('destroy --1--<br>');
- unset($obj->bar);
- unset($obj);
- }
- }
- class Bar {
- function __construct($foo) {
- $this->foo = $foo;
- }
- function __destruct() {
- echo('Bar die --2--<br>');
- }
- }
- for ($i = 0; $i < 100; $i++) {
- echo memory_get_usage(), "<br>";
- $obj = new Foo();
- //改用静态函数后,这里只要一行。
- Foo::destroy($obj);
- }
- echo memory_get_usage(), "<br>";
- ?>
PHP内存溢出 Allowed memory size of 解决办法的更多相关文章
- PHP内存溢出Allowed memory size of 解决办法
PHP内存溢出Allowed memory size of 解决办法 博客分类: php ============================Allowed memory size of x ...
- (转载)PHP的内存限制 Allowed memory size of 134217728 bytes exhausted (tried to allocate 1099 bytes) in
(转载)http://blog.csdn.net/beyondlpf/article/details/7794028 Fatal error: Allowed memory size of 13421 ...
- PHP的内存限制 Allowed memory size of 134217728 bytes exhausted (tried to allocate 1099 bytes) in
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 1099 bytes) in Fa ...
- Allowed memory size of 134217728 bytes exhausted解决办法(php内存耗尽报错)【简记】
报错: PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 72 bytes) i ...
- Fatal error: Allowed memory size of 524288000 bytes exhausted (tried to allocate 64 bytes) in D
Fatal error: Allowed memory size of 524288000 bytes exhausted (tried to allocate 64 bytes) in D 从数据库 ...
- Fatal error: Allowed memory size of 8388608 bytes exhausted
这两天安装bugfree,更换了一个数据量较大的库,结果打开bug详情页要么是空白页,要么就报如题的错误,错误信息还包括C:\wamp\www\bugfree\Include\Class\ADOLit ...
- 一次apk打开时报内存溢出错误,故写下内存溢出的各种原因和解决方法
原转载:https://blog.csdn.net/cp_panda_5/article/details/79613870 正文内容: 对于JVM的内存写过的文章已经有点多了,而且有点烂了,不过说那么 ...
- PHPExcel 报 Allowed memory size of 8388608 byte
使用 phpExcel 报 Allowed memory size of 8388608 bytes exhausted 错误,原因是php页面消耗的最大内存默认是为 8M (在PHP的ini件里可以 ...
- Allowed memory size of 134217728 bytes exhausted
错误信息: Allowed memory size of 134217728 bytes exhausted (tried to allocate 65015808 bytes) 由于报错信息和数据库 ...
随机推荐
- jQuery Mobile页面跳转后未加载外部JS原因分析及解决
在使用jQuery Mobile进行Web开发中,当页面跳转时(pageA => pageB),在pageB中引用的JS并未成功运行.因为,JQM并为将整个页面加载到当前的dom中,仅将data ...
- AJAX实现无刷新登录
最近学习了如何实现无刷新登录,大体的效果如下(界面比较丑,请自行忽略....): 点击登录按钮时弹出登录窗口,输入正确的用户名密码后点击登录则登录窗口关闭,状态改为当前用户名. 第一步: 首先弹出窗口 ...
- 【BZOJ2118】墨墨的等式(最短路)
[BZOJ2118]墨墨的等式(最短路) 题面 BZOJ 洛谷 题解 和跳楼机那题是一样的. 只不过走的方式从\(3\)种变成了\(n\)种而已,其他的根本没有区别了. #include<ios ...
- Error while uploading slice_4.apk : WARNING: linker: libhoudini.so has text relocations. This is wasting memory and prevents security hardening. Please fix.
问题: android studio 2.4版本新建项目后老是编译不过,查看log报如下错误,最后发现关闭即时编译功能可以解决此问题.这也算是2.4测试版的bug吧 android - Error w ...
- android中的style部分属性值介绍 --zz
Android平台定义的主题样式: android:theme="@android:style/Theme.Dialog" 将一个Activity显示为对话框模式 •andro ...
- c++优先队列(堆)
1.最小堆.最大堆 priority_queue<int,vector<int>,greater<int> > f; //最小堆(后面的数逐渐greater) pr ...
- dingo 内部调用获取异常信息
$exception->getResponse(); $params = [ 'company_id' => $this->request->input('company_id ...
- Python word_cloud 部分文档翻译 标签云系列(二)
转载地址:https://zhuanlan.zhihu.com/p/20436581上文末尾提到 Python 下还有一款词云生成器.amueller/word_cloud · GitHub 可以直接 ...
- 关于javaweb中图片的存储问题
图片上传到服务器,然后把上传路径保存到数据库,然后从数据库读出保存的路径显示到网站页面. 我们一般可以在CMS系统中将图片添加到图片服务器中(这个可以使用ftp来部署),然后图片上传到服务器后,在数据 ...
- etcd3集群管理
目录 在什么情况下需要集群的运行时更改 集群运行时更改的操作 更新一个节点 删除一个节点 增加一个节点 节点迁移和灾难恢复 迁移节点 灾难恢复 在什么情况下需要集群的运行时更改 维护和升级多个机器 如 ...