PHP記憶體溢出 Allowed memory size of 解決辦法

來源:互聯網
上載者:User

標籤: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代碼  
  1. <?php  
  2. class Foo {  
  3.     function __construct() {  
  4.         $this->bar = new Bar($this);  
  5.     }  
  6. }  
  7.   
  8. class Bar {  
  9.     function __construct($foo) {  
  10.         $this->foo = $foo;  
  11.     }  
  12. }  
  13.   
  14. for ($i = 0; $i < 100; $i++) {  
  15.     $obj = new Foo();  
  16.   
  17.     unset($obj);  
  18.   
  19.     echo memory_get_usage(), "\n";  
  20. }  
  21. ?>   

 

    運行以上代碼,你會發現,記憶體使用量量本應該不變才對,可實際上卻是不斷增加,unset沒有完全生效。
    我們看到,英文原文中給出的做法是,手工調用解構函式。為什麼要這樣做呢?
    我們給出一段新的代碼,注意一下,代碼中的注釋對問題的講解:

Php代碼  
  1. <?php  
  2. class Foo {  
  3.     function __construct() {  
  4.         //PHP5.3以前的版本,它是一個長生不老藥。因為建立了它,引用了自己。  
  5.         //實際上,只要外部有任一個對象的屬性中引用了這個類,這個類和引用它的類都不能被unset  
  6.         //這就是我們在這裡犯下了錯。  
  7.         $this->bar = new Bar($this);  
  8.     }  
  9.     function __destruct() {  
  10.         echo(‘Foo die --3--<br>‘);  
  11.         unset($this->bar);  
  12.     }  
  13.     /** 
  14.      * 因為我們在這裡犯下了錯,我們要在此收拾殘局。所以,添加一個收屍函數 
  15.      * 這一做法,也就是不要在外面調用__destruct,看上去很不雅觀。 
  16.      * 但增加此函數有一大好處。就是向使用者表明,需要我們主動把它殺掉。而不是等它自動死掉。 
  17.      * 如果不願意,你大可以直接在外部調用__destruct 
  18.      */  
  19.     function destroy()  
  20.     {  
  21.         echo(‘destroy --1--<br>‘);  
  22.         unset($this->bar);  
  23.     }  
  24. }  
  25.   
  26. class Bar {  
  27.     function __construct($foo) {  
  28.         $this->foo = $foo;  
  29.     }  
  30.       
  31.     function __destruct() {          
  32.         echo(‘Bar die --2--<br>‘);  
  33.     }  
  34. }  
  35.   
  36. for ($i = 0; $i < 100; $i++) {  
  37.     echo memory_get_usage(), "<br>";  
  38.     $obj = new Foo();  
  39.     //註解掉,或放開註解下面這一行,運行一下。結果分別在本文後面。  
  40.     //我們可以發現,如果我們不收屍,那麼,PHP主動調用__destruct,則是在程式結束時才執行。  
  41.     //如果我們主動做了,就能及時釋放記憶體。  
  42.     $obj->destroy();  
  43.     unset($obj);  
  44. }  
  45. echo memory_get_usage(), "<br>";  
  46. ?>   

 

    我們可以發現,當我們註解掉上面這段代碼中的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代碼  
  1. class Node {    
  2.     public $parentNode;    
  3.     public $childNodes = array();    
  4.     function Node() {    
  5.         $this->nodeValue = str_repeat(‘0123456789‘, 128);    
  6.     }    
  7. }    
  8. function createRelationship() {    
  9.     $parent = new Node();    
  10.     $child = new Node();    
  11.     $parent->childNodes[] = $child;    
  12.     $child->parentNode = $parent;    
  13. }   
  14. //And then let‘s review the amount of memory allocated by this script after 10,000 calls to createRelationship function.  
  15. echo ‘Initial: ‘ . number_format(memory_get_usage(), 0, ‘.‘, ‘,‘) . " bytes\n";    
  16. for($i = 0; $i < 10000; $i++) {    
  17.     createRelationship();    
  18. }    
  19. echo ‘Peak: ‘ . number_format(memory_get_peak_usage(), 0, ‘.‘, ‘,‘) . " bytes\n";    
  20. 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代碼  
  1. class Node {    
  2.     public $parentNode;    
  3.     public $childNodes = array();    
  4.     function Node() {    
  5.         $this->nodeValue = str_repeat(‘0123456789‘, 128);    
  6.     }    
  7.     function destroy()    
  8.     {    
  9.         $this->parentNode = null;    
  10.         $this->childNodes = array();    
  11.     }    
  12. }    
  13.   
  14. function createRelationship() {    
  15.     $parent = new Node();    
  16.     $child = new Node();    
  17.     $parent->childNodes[] = $child;    
  18.     $child->parentNode = $parent;    
  19.     $parent->destroy();    
  20. }   

 

    另外作者還給出了php5.3中的解決方案:使用php5.3中的新函數,手工調用gc_collect_cycles這個函數。

Php代碼  
  1. function createRelationship() {    
  2.     $parent = new Node();    
  3.     $child = new Node();    
  4.     $parent->childNodes[] = $child;    
  5.     $child->parentNode = $parent;    
  6.     gc_collect_cycles();    
  7. }   

 

    同時,作者也告訴了我們,調用gc_collect_cycles這個函數,我們不需要額外增加destroy了,但是效能上有一些缺陷。

    我們還可以對代碼作進一步改進。那就是改成靜態函數。具體如下:

Php代碼  
    1. <?php  
    2. class Foo {  
    3.     function __construct() {  
    4.         //PHP5.3以前的版本,它是一個長生不老藥。因為建立了它,引用了自己。  
    5.         //實際上,只要外部有任一個對象的屬性中引用了這個類,這個類和引用它的類都不能被unset  
    6.         //這就是我們在這裡犯下了錯。  
    7.         $this->bar = new Bar($this);  
    8.     }  
    9.     function __destruct() {  
    10.         echo(‘Foo die --3--<br>‘);  
    11.         unset($this->bar);  
    12.     }  
    13.     /** 
    14.      * 因為我們在這裡犯下了錯,我們要在此收拾殘局。所以,添加一個收屍函數 
    15.      * 這一做法,也就是不要在外面調用__destruct,看上去很不雅觀。 
    16.      * 但增加此函數有一大好處。就是向使用者表明,需要我們主動把它殺掉。而不是等它自動死掉。 
    17.      * 如果不願意,你大可以直接在外部調用__destruct 
    18.      * 實際上,這樣做以後,也就不用清理記憶體時,寫兩行代碼,一行代碼就夠了。 
    19.      */  
    20.     static function destroy($obj)  
    21.     {  
    22.         echo(‘destroy --1--<br>‘);  
    23.         unset($obj->bar);  
    24.         unset($obj);  
    25.     }  
    26. }  
    27.   
    28. class Bar {  
    29.     function __construct($foo) {  
    30.         $this->foo = $foo;  
    31.     }  
    32.       
    33.     function __destruct() {          
    34.         echo(‘Bar die --2--<br>‘);  
    35.     }  
    36. }  
    37.   
    38. for ($i = 0; $i < 100; $i++) {  
    39.     echo memory_get_usage(), "<br>";  
    40.     $obj = new Foo();  
    41.     //改用靜態函數後,這裡只要一行。  
    42.     Foo::destroy($obj);  
    43.   
    44. }  
    45. echo memory_get_usage(), "<br>";  
    46. ?>   

PHP記憶體溢出 Allowed memory size of 解決辦法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.