好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > 關于Yii2框架跑腳本時內存泄漏問題的分析與解決

關于Yii2框架跑腳本時內存泄漏問題的分析與解決

熱門標簽:杭州語音電銷機器人 江門回撥外呼系統 高德地圖標注位置怎么標注 電銷機器人沒有效果怎么樣 天潤融通外呼系統好嗎 泊頭在哪里辦理400電話 江西電銷機器人收費 欣思維地圖標注 高德地圖標注店鋪收費嗎

現象

在跑 edu_ocr_img 表的歸檔時,每跑幾萬個數據,都會報一次內存耗盡

PHP Fatal error:  Allowed memory size of 134217728 bytesexhausted (tried toallocate 135168 bytes)

跟蹤代碼發現,是在插入時以下代碼造成的:

EduOCRTaskBackup::getDb()->createCommand()->batchInsert(EduOCRTaskBackup::tableName(), $fields, $data)->execute();

execute 之后會造成使用內存漲上去,并且在之后 unset 所有變量內存也會有一部分不會刪除,直到內存耗盡。

于是跟蹤到 Yii2中execute的具體代碼塊發現在記錄 log 的時候會將使用很高的內存,分析代碼之后得出造成泄漏的代碼塊如下:

造成泄漏的代碼塊

/**
 * 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;
    }
   }
  }
 }
 
 // 這里是造成內存的罪魁禍首
 $this->messages[] = [$message, $level, $category, $time, $traces];
 if ($this->flushInterval > 0  count($this->messages) >= $this->flushInterval) {
  $this->flush();
 }
}

造成內存泄漏的原因分析

在 Yii2框架中的 vendor/yiisoft/yii2/log/Logger.php:156 log函數的156行之后會判斷 count($this->messages) >= $this->flushInterval

即:內存中存儲的 message 的條數要大于等于預設的 $this->flushInterval 才會將內存中的message 刷到磁盤上去。

如果在刷新到磁盤之前就已經將 php.ini 設置的 128M 內存打滿的話,會直接報錯申請內存耗盡。

很多關于 YII2其他原因的內存泄漏的討論
https://github.com/yiisoft/yii2/issues/13256

解決方案

在程序開始時,設置 flushInterval 為一個比較小的值

\Yii::getLogger()->flushInterval = 100; // 設置成一個較小的值

在程序執行過程中,每次 execute 之后對內存中的 message 進行 flush

\Yii::getLogger()->flush(true); // 參數傳 true 表示每次都會將 message 清理到磁盤中

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。

您可能感興趣的文章:
  • 從零開始學YII2框架(一)通過Composer安裝Yii2框架
  • 從零開始學YII2框架(五)快速生成代碼工具 Gii 的使用
  • 從零開始學YII2框架(六)高級應用程序模板
  • 從零開始學YII2框架(三)擴展插件yii2-gird
  • 從零開始學YII2框架(二)通過 Composer 安裝擴展插件
  • 從零開始學YII2框架(四)擴展插件yii2-kartikgii
  • Yii2框架數據庫簡單的增刪改查語法小結
  • Yii2框架使用計劃任務的方法

標簽:大同 深圳 雙鴨山 平涼 江門 駐馬店 石嘴山 內江

巨人網絡通訊聲明:本文標題《關于Yii2框架跑腳本時內存泄漏問題的分析與解決》,本文關鍵詞  關于,Yii2,框架,跑,腳本,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《關于Yii2框架跑腳本時內存泄漏問題的分析與解決》相關的同類信息!
  • 本頁收集關于關于Yii2框架跑腳本時內存泄漏問題的分析與解決的相關信息資訊供網民參考!
  • 推薦文章