定时器调用getAllClientCount和getAllClientSessions,导致内存溢出

liuping0207

问题描述

在onWorkerStart($worker)中启动一个10S定时器,该定时器每次调用getAllClientCount和getAllClientSessions查询连接状态;结果每次调用内存都在不断的增长,最后导致内存不够进程复位;执行打印结果参考:
Print_Session_Info -----start-----使用: 8.66Mb
Print_Session_Info -----middle1------使用: 8.69Mb
Print_Session_Info -----middle1_1----使用: 8.71Mb
Print_Session_Info -----middle2------使用: 8.74Mb
Print_Session_Info -----middle2_1----使用: 8.76Mb


这里粘代码或配置
系统:Alibaba Cloud (Aliyun ) 2.1903 LTS (Hunting Beagle) x86_64(Py3.7.9)
Workerman version:4.0.40          PHP version:7.4.30
    public static function onWorkerStart($worker)
    {

            Timer::add(10, function () {
            Events::Print_Session_Info(1);
        });
    }

    public static function Print_Session_Info($flag)
    {
        echo "\nPrint_Session_Info -----start-----使用: ".number_format(memory_get_usage()/1024/1024, '2' , $dec_point = '.' , $thousands_sep = '')."Mb\n";
        Util::logger(Util::LOG_INFO,Util::DEV_GATEWAY_LP,"\n");
        Util::logger(Util::LOG_INFO,Util::DEV_GATEWAY_LP,">>>>>>>getAllClientCount: ".print_r(Gateway::getAllClientCount(),true));
        echo "\nPrint_Session_Info -----middle1------使用: ".number_format(memory_get_usage()/1024/1024, '2' , $dec_point = '.' , $thousands_sep = '')."Mb\n";
        Gateway::getAllClientCount();
        echo "\nPrint_Session_Info -----middle1_1----使用: ".number_format(memory_get_usage()/1024/1024, '2' , $dec_point = '.' , $thousands_sep = '')."Mb\n";
        $sessino_info = Gateway::getAllClientSessions();
        echo "\nPrint_Session_Info -----middle2------使用: ".number_format(memory_get_usage()/1024/1024, '2' , $dec_point = '.' , $thousands_sep = '')."Mb\n";
        Gateway::getAllClientSessions();
        echo "\nPrint_Session_Info -----middle2_1----使用: ".number_format(memory_get_usage()/1024/1024, '2' , $dec_point = '.' , $thousands_sep = '')."Mb\n";
    }
916 1 1
1个回答

walkor 打赏

Print_Session_Info方法里的 Util::logger() 调用去掉试下

  • liuping0207 2023-04-18

    测试结果还是一样的:

       echo "\nPrint_Session_Info -----start-----使用: ".number_format(memory_get_usage()/1024/1024, '2' , $dec_point = '.' , $thousands_sep = '')."Mb\n";
        Gateway::getAllClientCount();
        echo "\nPrint_Session_Info -----middle1------使用: ".number_format(memory_get_usage()/1024/1024, '2' , $dec_point = '.' , $thousands_sep = '')."Mb\n";
        Gateway::getAllClientCount();
        gc_collect_cycles();
        echo "\nPrint_Session_Info -----middle1_1----使用: ".number_format(memory_get_usage()/1024/1024, '2' , $dec_point = '.' , $thousands_sep = '')."Mb\n";
        $sessino_info = Gateway::getAllClientSessions();
        gc_collect_cycles();
        echo "\nPrint_Session_Info -----middle2------使用: ".number_format(memory_get_usage()/1024/1024, '2' , $dec_point = '.' , $thousands_sep = '')."Mb\n";
        Gateway::getAllClientSessions();
        gc_collect_cycles();
        echo "\nPrint_Session_Info -----middle2_1----使用: ".number_format(memory_get_usage()/1024/1024, '2' , $dec_point = '.' , $thousands_sep = '')."Mb\n";

    Print_Session_Info -----start-----使用: 3.59Mb
    Print_Session_Info -----middle1------使用: 3.61Mb
    Print_Session_Info -----middle1_1----使用: 3.64Mb
    Print_Session_Info -----middle2------使用: 3.67Mb
    Print_Session_Info -----middle2_1----使用: 3.69Mb

  • walkor 2023-04-18

    我这测试没问题

  • liuping0207 2023-04-18

    会不会是PHP版本的问题

  • liuping0207 2023-04-18

    同样的代码,我在我另外一台windows服务器上是正常的

年代过于久远,无法发表回答
×
🔝