守护进程无法内存回收?

supereric

我在一个定时器中执行函数类似下图
[attach]1811[/attach]
 
然后内存会一直蹭蹭往上涨(循环1000次是为了模拟超长时间下运行)
 
刚开始是这样,内存占用情况
[attach]1812[/attach]
 
运行一段时间后,变成了这样
[attach]1813[/attach]
 
只要一直不断运行下去,内存就会无限涨下去直到出现了错误提示
[attach]1814[/attach]
 
msg_send() 函数内部大概这样,下面就没什么了,就是个发送的循环
[attach]1815[/attach]
 
我的问题是,执行函数的时候产生的内存消耗是没有回收的吗?只能让它运行到爆炸了?
这个问题我搜了好久,大致都是说php会自动回收内存,但是需要脚本运行结束后触发回收
可是我是常驻内存的,如何定义为脚本结束呢?
 
在实际案例中已经产生的问题,特此用这种极端测试法重现效果。
 

4634 2 0
2个回答

supereric

问题解决啦!不是workerman的问题!
我把源代码下载下来后按案例去做了一便,同样的执行代码

<?php
use Workerman\Worker;
require_once './Workerman/Autoloader.php';
class se{
    public $worker;
    public function __construct(){
        $this->worker = new Worker("http://0.0.0.0:2345");
        $this->worker->count = 1;
        $fn = ;
        foreach ($fn as $event) {
            if (method_exists($this, $event)) {
                $this->worker->$event = ;
            }
        }

        Worker::runAll();
    }
    public function onWorkerStart(){
        \Workerman\Lib\Timer::add(1, function(){
            for($i = 0;$i < 1000;$i++){
                    $this->msg_send();
            }
        });
    }
    public function msg_send($type = 'sys',$content = '',$data = []){
        $info    = $type;
        $info    = time();
        $info = $content;
        $info  = $data;
    }
}

new se();

发现根本不是workerman问题,更不是程序问题,是我整合的thinkphp3.2.3出了问题![attach]1816[/attach]
 
感谢这篇文章的博主,帮我解决了这个问题
https://blog.csdn.net/qq1509334304/article/details/82775414
根据他的修改方案
找到Log.class.php修改以下函数,照着这个改

static function record($message,$level=self::ERR,$record=false) {
    if($record || false !== strpos(C('LOG_LEVEL'),$level)) {
        self::$log[] =   "{$level}: {$message}\r\n";

        if(count(self::$log) > 100){
            self::save();
            self::$log = [];
        }

    }

}

便可以解决问题啦!

  • 暂无评论
yongdao35

学习了

  • 暂无评论
年代过于久远,无法发表回答
🔝