簡體   English   中英

2個foreach循環內使用“ continue 2”時,php 5.5內存泄漏?

[英]php 5.5 memory leak when using “continue 2” inside two foreach loops?

我剛遇到一個奇怪的內存泄漏問題。 我設法將問題歸結為以下代碼:

<?php
$data=array();
for($c=0; $c<32768; $c++)
    $data[$c]=array(1, 2, 3);
$filter=array(1, 2, 3);

for($kc=0; $kc<25600; $kc++)
{
    $cm=memory_get_usage(true);
    $pm=memory_get_peak_usage(true);
    echo "<b>loop $kc: current_memory: $cm, peak_memory: $pm...</b><br>";
    flush();

    foreach($data as $entry)
        foreach($filter as $pattern)
            continue 2;
}
?>

輸出:

loop 0: current_memory: 12582912, peak_memory: 12582912...
loop 1: current_memory: 20709376, peak_memory: 20709376...
loop 2: current_memory: 28835840, peak_memory: 28835840...
loop 3: current_memory: 36962304, peak_memory: 36962304...
loop 4: current_memory: 45088768, peak_memory: 45088768...
loop 5: current_memory: 53215232, peak_memory: 53215232...
loop 6: current_memory: 61341696, peak_memory: 61341696...
loop 7: current_memory: 69468160, peak_memory: 69468160...
loop 8: current_memory: 77594624, peak_memory: 77594624...
loop 9: current_memory: 85721088, peak_memory: 85721088...
loop 10: current_memory: 93847552, peak_memory: 93847552...
loop 11: current_memory: 101974016, peak_memory: 101974016...
loop 12: current_memory: 110100480, peak_memory: 110100480...
loop 13: current_memory: 118226944, peak_memory: 118226944...
loop 14: current_memory: 126353408, peak_memory: 126353408...
loop 15: current_memory: 134479872, peak_memory: 134479872...
loop 16: current_memory: 142606336, peak_memory: 142606336...
loop 17: current_memory: 151257088, peak_memory: 151257088...
loop 18: current_memory: 159383552, peak_memory: 159383552...
loop 19: current_memory: 167510016, peak_memory: 167510016...
loop 20: current_memory: 175636480, peak_memory: 175636480...
loop 21: current_memory: 183762944, peak_memory: 183762944...
loop 22: current_memory: 191889408, peak_memory: 191889408...
loop 23: current_memory: 200015872, peak_memory: 200015872...
loop 24: current_memory: 208142336, peak_memory: 208142336...
loop 25: current_memory: 216268800, peak_memory: 216268800...
loop 26: current_memory: 224395264, peak_memory: 224395264...
loop 27: current_memory: 232521728, peak_memory: 232521728...
loop 28: current_memory: 240648192, peak_memory: 240648192...
loop 29: current_memory: 248774656, peak_memory: 248774656...
loop 30: current_memory: 256901120, peak_memory: 256901120...
loop 31: current_memory: 265027584, peak_memory: 265027584...
Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 40 bytes) in xxx

當我刪除“ continue 2”語句並使用簡單的“ break”或“ continue”時,內存使用保持不變。 誰能確認這種奇怪的行為? 我是否在php 5.5中發現了內存泄漏錯誤? 使用php-cli不會顯示這種奇怪的行為。 僅將php作為apache內的mod_php使用可顯示此行為。

我的系統:

$ php --version
PHP 5.5.11-2 (cli) (built: Apr  8 2014 11:42:22) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
    with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2014, by Zend Technologies

$ apache2ctl status
Apache Server Status for localhost (via ::1)

   Server Version: Apache/2.4.9 (Debian)
          mod_fastcgi/mod_fastcgi-SNAP-0910052141 PHP/5.5.11-2
          mod_perl/2.0.8 Perl/v5.18.2

   Server MPM: prefork
   Server Built: Mar 29 2014 21:52:01

我正在進行當前的Debian測試(傑西)。

謝謝你的幫助!

等待!

您不應該禁用opcache:除了緩存之外,opcache還會執行優化。

優化意味着opcache會更改已編譯的操作碼

我敢肯定,優化優化會向您顯示引入錯誤的位置,這意味着您可以在bugs.php.net上創建錯誤報告,我們有機會為每個人解決問題。

請花些時間做這個。

優化優化

Opcache具有多個優化過程, https: //gist.github.com/krakjoe/962e54c38b155f896b00

配置運行過程是在配置中更改opcache.optimization_level的問題,這是一個位掩碼,默認情況下為0xffffffff

如果您需要調試幫助,請訪問EFnet上的#php.pecl。

注意:要在php-cli中啟用opcache.enable_cliopcache.enable_cli更改為1 ,這將允許您在控制台中進行測試,應顯示相同的行為

僅出於記錄目的,仍然在5.5和5.6.3( https://bugs.php.net/bug.php?id=65743 )下驗證了“ continue 2”錯誤。 一種解決方法是利用“ goto-hack”而不是繼續,而是在第一個foreach中“返回”到已定義的標簽...我知道,f * gly但可以( opcache 一起使用)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM