關(guān)于Yii2框架跑腳本時內(nèi)存泄漏問題的分析與解決
現(xiàn)象
在跑 edu_ocr_img 表的歸檔時,每跑幾萬個數(shù)據(jù),都會報一次內(nèi)存耗盡
PHP Fatal error: Allowed memory size of 134217728 bytesexhausted (tried toallocate 135168 bytes)
跟蹤代碼發(fā)現(xiàn),是在插入時以下代碼造成的:
EduOCRTaskBackup::getDb()->createCommand()->batchInsert(EduOCRTaskBackup::tableName(), $fields, $data)->execute();
execute 之后會造成使用內(nèi)存漲上去,并且在之后 unset 所有變量內(nèi)存也會有一部分不會刪除,直到內(nèi)存耗盡。
于是跟蹤到 Yii2中execute的具體代碼塊發(fā)現(xiàn)在記錄 log 的時候會將使用很高的內(nèi)存,分析代碼之后得出造成泄漏的代碼塊如下:
造成泄漏的代碼塊
/** * Logs a message with the given type and category. * If [[traceLevel]] is greater than 0, additional call stack information about * the application code will be logged as well. * @param string|array $message the message to be logged. This can be a simple string or a more * complex data structure that will be handled by a [[Target|log target]]. * @param integer $level the level of the message. This must be one of the following: * `Logger::LEVEL_ERROR`, `Logger::LEVEL_WARNING`, `Logger::LEVEL_INFO`, `Logger::LEVEL_TRACE`, * `Logger::LEVEL_PROFILE_BEGIN`, `Logger::LEVEL_PROFILE_END`. * @param string $category the category of the message. */ public function log($message, $level, $category = 'application') { $time = microtime(true); $traces = []; if ($this->traceLevel > 0) { $count = 0; $ts = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); array_pop($ts); // remove the last trace since it would be the entry script, not very useful foreach ($ts as $trace) { if (isset($trace['file'], $trace['line']) && strpos($trace['file'], YII2_PATH) !== 0) { unset($trace['object'], $trace['args']); $traces[] = $trace; if (++$count >= $this->traceLevel) { break; } } } } // 這里是造成內(nèi)存的罪魁禍首 $this->messages[] = [$message, $level, $category, $time, $traces]; if ($this->flushInterval > 0 && count($this->messages) >= $this->flushInterval) { $this->flush(); } }
造成內(nèi)存泄漏的原因分析
在 Yii2框架中的 vendor/yiisoft/yii2/log/Logger.php:156 log函數(shù)的156行之后會判斷 count($this->messages) >= $this->flushInterval
即:內(nèi)存中存儲的 message 的條數(shù)要大于等于預(yù)設(shè)的 $this->flushInterval 才會將內(nèi)存中的message 刷到磁盤上去。
如果在刷新到磁盤之前就已經(jīng)將 php.ini 設(shè)置的 128M 內(nèi)存打滿的話,會直接報錯申請內(nèi)存耗盡。
很多關(guān)于 YII2其他原因的內(nèi)存泄漏的討論
https://github.com/yiisoft/yii2/issues/13256
解決方案
在程序開始時,設(shè)置 flushInterval 為一個比較小的值
\Yii::getLogger()->flushInterval = 100; // 設(shè)置成一個較小的值
在程序執(zhí)行過程中,每次 execute 之后對內(nèi)存中的 message 進行 flush
\Yii::getLogger()->flush(true); // 參數(shù)傳 true 表示每次都會將 message 清理到磁盤中
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。
相關(guān)文章
PHP遞歸遍歷指定文件夾內(nèi)的文件實現(xiàn)方法
下面小編就為大家?guī)硪黄狿HP遞歸遍歷指定文件夾內(nèi)的文件實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11Zend Framework教程之配置文件application.ini解析
這篇文章主要介紹了Zend Framework教程之配置文件application.ini用法,分析了配置文件application.ini中常見的配置項含義及用法,需要的朋友可以參考下2016-03-03Yii2使用$this->context獲取當前的Module、Controller(控制器)、Action等
使用Yii2的時候,在某些場景和環(huán)境下需要獲得Yii2目前所處于的module(模型)、Controller(控制器)、Action(方法),以及會調(diào)用控制器里面已經(jīng)定義過的一些公共的方法等.對于這些問題Yii2可以在視圖層View中使用$this->context這個對象去獲得。下面通過示例代碼給大家講解下2017-03-03ThinkPHP數(shù)據(jù)操作方法總結(jié)
這篇文章主要介紹了ThinkPHP數(shù)據(jù)操作方法,以實例形式較為詳細的分析總結(jié)了ThinkPHP添加、更新、查詢及刪除數(shù)據(jù)的實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-09-09