swoft-cloud / swoft

🚀 PHP Microservice Full Coroutine Framework
https://swoft.org
Apache License 2.0
5.58k stars 786 forks source link

疑似swoft1.0内存泄露问题 #1024

Closed JoeXiong closed 5 years ago

JoeXiong commented 5 years ago
Q A
Bug report? yes/no
Feature request? yes/no
Swoft version 1.0.0
Swoole version 4.3.4 or 4.4.4
PHP version 7.2.11 or 7.3.6
Runtime environment CentOS Linux release 7.4.1708 (Core)

swoft1.0版本中开启了自定义进程。使用了swoole_tracker检测内存泄露问题

  1. 自定义进程内存一直不断增加 TIM截图20191009113413

  2. 泄露的信息(这个是work进程上传的信息): `

0 /data/release/swoft/data_processing_center/ecs/apps/app/MessageResponse/Production/Rules/RuleCenter.php:465#1 /data/release/swoft/data_processing_center/ecs/apps/app/MessageResponse/Production/Bean/Data.php:146#2 /data/release/swoft/data_processing_center/ecs/apps/app/MessageResponse/Production/Bean/Data.php:164#3 /data/release/swoft/data_processing_center/ecs/apps/app/MessageResponse/Production/Bean/Data.php:524#4 /data/release/swoft/data_processing_center/ecs/apps/app/MessageResponse/Consumption/Tasks/CommonConTask.php:51#5 /data/release/swoft/data_processing_center/ecs/base/vendor/swoft/api/src/BaseApi.php:15#6 /data/release/swoft/data_processing_center/ecs/apps/app/MessageResponse/Production/Rules/RuleCenter.php:120#7 /data/release/swoft/data_processing_center/ecs/apps/app/MessageResponse/Production/Rules/RuleCenter.php:121#8 /data/release/swoft/data_processing_center/ecs/apps/app/MessageResponse/Production/Rules/RuleCenter.php:122#9 /data/release/swoft/data_processing_center/ecs/apps/app/MessageResponse/Production/Rules/RuleCenter.php:128#10 /data/release/swoft/data_processing_center/ecs/apps/app/MessageResponse/Production/Rules/RuleCenter.php:153#11 /data/release/swoft/data_processing_center/ecs/apps/app/MessageResponse/Production/Rules/RuleCenter.php:155#12 /data/release/swoft/data_processing_center/ecs/base/vendor/swoft/rpc-server/src/BaseService.php:15#13 /data/release/swoft/data_processing_center/ecs/base/vendor/swoft/redis/src/Operator/Processor/PrefixProcessor.php:163#14 /data/release/swoft/data_processing_center/ecs/base/vendor/swoft/framework/src/Log/Logger.php:369#15 /data/release/swoft/data_processing_center/ecs/base/vendor/swoft/framework/src/Log/Logger.php:134#16 /data/release/swoft/data_processing_center/ecs/base/vendor/swoft/framework/src/Log/Logger.php:416 | #0 data#1 orderId#2 orderType#3 defaultTime#4 repeat#5 params#6 department#7 taskName#8 abnormalType#9 status#10 real_valid_time#11 tip_type#12 params#13 prefix#14 messages#15 0#16 1

-- | -- `

一个是Logger.php 中 $this->messages 其他的泄露信息都是对Bean单例模式下,设置对象属性时提示的,都是类似下面的形式: public function setData(Data $data) { $this->data = $data; return $this; }

stelin commented 5 years ago

建议升级到2.0,同时这里不不会导致内存泄漏,你需要从业务排除

JoeXiong commented 5 years ago

@stelin 那请问怎么业务排除,应该从哪些方面去检查排除?

JoeXiong commented 5 years ago

之前在再检查这个问题的时候增加了一个处理:在每一次循环后都增加了一次清除 public function clean() { RequestContext::destroy(); App::getLogger()->appendNoticeLog(); } 后内存增长量基本可以忽略了,后因为App::getLogger()->appendNoticeLog();每次循环都增加了一条请求日志,垃圾日志太多,后以为是$this->messages的原因,把appendNoticeLog()替换成flushLog(),观察到现在内存增长量特别明显。现在把flushLog()还原为appendNoticeLog()内存增长量恢复正常。

现有两个问题: 1.swoole_tracker内存泄漏检测是不是不能对自定义进程监控?

2.比较了下appendNoticeLog()和flushLog()的区别 Logger.php 411行 有一行unset的操作 unset($this->profiles[$cid], $this->countings[$cid], $this->pushlogs[$cid], $this-profileStacks[$cid]); 是不是和这个有关?

stelin commented 5 years ago

你把 flushLog 的日志条数 调整低一点试试

stelin commented 5 years ago

每一条就输出