久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網
      最全最豐富的資訊網站

      php遞歸內存怎么釋放

      php遞歸內存怎么釋放

      如果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(), " "; } ?>

      運行以上代碼,你會發(fā)現,內存使用量本應該不變才對,可實際上卻是不斷增加,unset沒有完全生效。

      現在的開發(fā)很多都是基于框架進行的,應用里存在復雜的對象關系,那么就很可能會遇到這樣的問題,下面看看有什么權宜之計:

      <?php class Foo {     function __construct() {         $this->bar = new Bar($this);     }       function __destruct() {         unset($this->bar);     } }   class Bar {     function __construct($foo) {         $this->foo = $foo;     } }   for ($i = 0; $i < 100; $i++) {     $obj = new Foo();       $obj->__destruct();       unset($obj);       echo memory_get_usage(), " "; } ?>

      幸運的是這個Bug在PHP5.3的CVS代碼中已經被修復了。

      遞歸終止條件,一般有多種方式:

      1. 添加遞歸深度參數到遞歸函數的參數中

      每次調用深度加一,在函數體中添加條件語句,當深度超過某個值時強行return;

      2. 引入元素棧結構,每次遞歸的一些需要記錄的內容,通常會壓入棧中,適當的時候再彈出

      在函數體中,添加條件語句,判斷棧大小或者棧元素,達到條件時進行return;

      贊(0)
      分享到: 更多 (0)
      網站地圖   滬ICP備18035694號-2    滬公網安備31011702889846號