Yii2框架下运行代码内存泄漏的情况怎么办
Admin 2022-06-29 群英技术资讯 1160 次浏览
这篇文章给大家分享的是Yii2框架下运行代码内存泄漏的情况怎么办。小编觉得挺实用的,因此分享给大家做个参考,文中的介绍得很详细,而要易于理解和学习,有需要的朋友可以参考,接下来就跟随小编一起了解看看吧。现象
在跑 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 清理到磁盘中
总结
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
一一得一;一二得二二二得四;一三得三二三得六三三得九......相信九九乘法表口诀已经刻入每个中国人的骨髓了吧哈哈~不过今天咱们来写一个六六乘法表,主要用PHP来实现输出!其实实现的方法是非常简单的,主要希望这样的思路能让大家在其他开发过程中能举一反三!
swoole使用纯C编写,不依赖其他第三方库;swoole并没有用libevent,所以不需要安装libevent;swoole并不依赖php的stream/sockets/pcntl/posix/sysvmsg等扩展。
filter_has_var(type, variable) 是否存在指定类型的变量。filter_input 从脚本外部获取输入,并进行过滤。filter_input_array 从脚本外部获取多项输入,并进行过滤。
首先定义一个findArea()方法,参数r为圆的半径,圆周率π取3.142,函数的返回值为PI * (r*r),然后调用该方法即可求出圆的面积。
swoole 1.7.9增加了内置的WebSocket服务器支持,如使用此命令:$server = new Swoole\WebSocket\Server("0.0.0.0", 9501);。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008