標籤:des blog http io ar 使用 sp for on
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代碼
- <?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代碼
- <?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
其中給出的代碼:
Php代碼
- 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方法,
Php代碼
- 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這個函數。
Php代碼
- function createRelationship() {
- $parent = new Node();
- $child = new Node();
- $parent->childNodes[] = $child;
- $child->parentNode = $parent;
- gc_collect_cycles();
- }
同時,作者也告訴了我們,調用gc_collect_cycles這個函數,我們不需要額外增加destroy了,但是效能上有一些缺陷。
我們還可以對代碼作進一步改進。那就是改成靜態函數。具體如下:
Php代碼
- <?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 解決辦法