久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放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. 站長(zhǎng)資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      php遞歸內(nèi)存怎么釋放

      php遞歸內(nèi)存怎么釋放

      如果PHP對(duì)象存在遞歸引用,就會(huì)出現(xiàn)內(nèi)存泄漏。這個(gè)Bug在PHP里已經(jīng)存在很久很久了,先讓我們來(lái)重現(xiàn)這個(gè)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(), " "; } ?>

      運(yùn)行以上代碼,你會(huì)發(fā)現(xiàn),內(nèi)存使用量本應(yīng)該不變才對(duì),可實(shí)際上卻是不斷增加,unset沒(méi)有完全生效。

      現(xiàn)在的開(kāi)發(fā)很多都是基于框架進(jìn)行的,應(yīng)用里存在復(fù)雜的對(duì)象關(guān)系,那么就很可能會(huì)遇到這樣的問(wèn)題,下面看看有什么權(quán)宜之計(jì):

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

      幸運(yùn)的是這個(gè)Bug在PHP5.3的CVS代碼中已經(jīng)被修復(fù)了。

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

      1. 添加遞歸深度參數(shù)到遞歸函數(shù)的參數(shù)中

      每次調(diào)用深度加一,在函數(shù)體中添加條件語(yǔ)句,當(dāng)深度超過(guò)某個(gè)值時(shí)強(qiáng)行return;

      2. 引入元素棧結(jié)構(gòu),每次遞歸的一些需要記錄的內(nèi)容,通常會(huì)壓入棧中,適當(dāng)?shù)臅r(shí)候再?gòu)棾?/p>

      在函數(shù)體中,添加條件語(yǔ)句,判斷棧大小或者棧元素,達(dá)到條件時(shí)進(jìn)行return;

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