swoft-cloud / swoft

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

redis 在 timer 内使用过程中内存溢出 #310

Closed jianhuihi closed 6 years ago

jianhuihi commented 6 years ago

swoft: 1.0.0, php: 7.2.7, swoole: 4.0.1

swoole_timer_tick(3, function () {
            $memory = 0;
            $redis = new \Swoft\Redis\Redis();
            $redis->getMultiple('a', 'b');
            unset($redis);
            echo "\n memory use :" . (memory_get_usage() - $memory);
            echo "\t" . $memory = memory_get_usage();
        });

运行脚本会发现内存持续增加


 memory use :19795272   19795272
++++++++end(array)++++++++++++

 memory use :19796720   19796720
++++++++end(array)++++++++++++

 memory use :19798168   19798168
++++++++end(array)++++++++++++

 memory use :19799616   19799616
++++++++end(array)++++++++++++

 memory use :19812344   19812344
++++++++end(array)++++++++++++

 memory use :19802512   19802512
++++++++end(array)++++++++++++

 memory use :19803960   19803960
++++++++end(array)++++++++++++

 memory use :19805408   19805408
++++++++end(array)++++++++++++

 memory use :19806856   19806856
++++++++end(array)++++++++++++

中间省略N行

 memory use :21215456   21215456
++++++++end(array)++++++++++++

 memory use :21205624   21205624
++++++++end(array)++++++++++++

 memory use :21218352   21218352
++++++++end(array)++++++++++++

 memory use :21208520   21208520
++++++++end(array)++++++++++++

 memory use :21221248   21221248
++++++++end(array)++++++++++++

 memory use :21211416   21211416
++++++++end(array)++++++++++++

 memory use :21212864   21212864
++++++++end(array)++++++++++++

 memory use :21214312   21214312
++++++++end(array)++++++++++++

 memory use :21215760   21215760
++++++++end(array)++++++++++++

 memory use :21217208   21217208
++++++++end(array)++++++++++++

 memory use :21218656   21218656
++++++++end(array)++++++++++++

memory use :130834744   130834744
++++++++end(array)++++++++++++
PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes) in /code/test/vendor/swoft/framework/src/Proxy/Proxy.php(44) : eval()'d code on line 17

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes) in /code/test/vendor/swoft/framework/src/Proxy/Proxy.php(44) : eval()'d code on line 17
You have
huangzhhui commented 6 years ago

每次 timer_tick 会开启一个新的协程,在操作Redis的时候会使用到连接池,连接池会使用到RequestContext,顾RequestContext内会持续储存上下文信息,而没有得到释放,所以需在 timer_tick 内主动调用 RequestContext::destroy() 来释放上下文信息