phpsocket.io icon indicating copy to clipboard operation
phpsocket.io copied to clipboard

Memory Usage

Open Xuphey opened this issue 6 years ago • 12 comments

Hi, I'm currently using web-msg-sender on my site, after a running phpsocket.io on workerman for like 20 days, i see it consumes 8G of memory and rising. Does it look normal to you?


#top PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
100276 webuser 20 0 9022708 8.054g 5724 S 0.3 6.4 53:24.00 php


Workerman[start.php] status ---------------------------------------GLOBAL STATUS-------------------------------------------- Workerman version:3.4.5 PHP version:5.6.30 start time:2017-09-15 11:48:28 run 24 days 4 hours
load average: 0.03, 0, 0 event-loop:\Workerman\Events\Select 1 workers 1 processes worker_name exit_status exit_count PHPSocketIO 0 0 ---------------------------------------PROCESS STATUS------------------------------------------- pid memory listening worker_name connections total_request send_fail throw_exception 100276 17M socketIO://0.0.0.0:8008 PHPSocketIO 42 2791276 6 0

Xuphey avatar Oct 10 '17 02:10 Xuphey

I restart the daemon last night this is how it looks in the morning

#top PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
170897 webuser 20 0 949452 386376 6876 S 0.0 0.3 2:18.38 php

Workerman[start.php] status ---------------------------------------GLOBAL STATUS-------------------------------------------- Workerman version:3.4.5 PHP version:5.6.30 start time:2017-10-09 16:44:23 run 0 days 17 hours
load average: 0.15, 0, 0 event-loop:\Workerman\Events\Select 1 workers 1 processes worker_name exit_status exit_count PHPSocketIO 0 0 ---------------------------------------PROCESS STATUS------------------------------------------- pid memory listening worker_name connections total_request send_fail throw_exception 170897 13.5M socketIO://0.0.0.0:2120 PHPSocketIO 38 97814 1 0

Xuphey avatar Oct 10 '17 02:10 Xuphey

It doesn't look normal. There may be a memory leak somewhere for web-msg-sender. I'm not sure.

walkor avatar Oct 10 '17 05:10 walkor

but the pid belongs to phpsocketio

top shows it uses 8g in start.php status message it uses only 17M

Xuphey avatar Oct 10 '17 09:10 Xuphey

17M is the result of php function memory_get_usage(true). I don't know why there is such a big difference.

walkor avatar Oct 10 '17 14:10 walkor

After 40 hours of running, the usage is over 1g now, i also kept track of several performance data in the log, see the chart below

screen shot 2017-10-13 at 10 29 59 am

I don't know if it's obvious enough to you the memory usage (blue), is somehow related to alive connection(green)/timer(purple), when there is connection established, also timer is created, the memory usage goes up, but never come back down when connection closes

memory usage is from "$top", connection/timer data is from "$php start.php status" x-axis is number of data logging(infinite loop interval 3 seconds), y-axis is data logged

Xuphey avatar Oct 13 '17 02:10 Xuphey

Thank you so much for your chart.Yes, it seems there is memory leak with connection or timer. The mechanism of phpsocket.io is so complex, and it is not easy to find out the problem. There are too many circular references in phpsocket.io. I tried to figure out the problem, but it didn't work very well. I think it's necessary to refactor phpsocket.io to solve this problem.

walkor avatar Oct 13 '17 03:10 walkor

@walkor is this fixed?

Under disconnect, are you destroying the socket of the previous socket? I guess that's the problem here, maybe do some post-destroy on disconnect.

Imagine $socket->user = (object) that's a lot of memory usage when using objects specially if you have a lot of user's connected.

daison12006013 avatar May 17 '18 11:05 daison12006013

Unfortunately, it has not been fixed yet. Socket is destroyed when the connection closed.

walkor avatar May 18 '18 02:05 walkor

hello walkor hope you are good. facing issue in this library. working perfectly but when increase number of active users current PID will changed and running process terminated. For Example current PID is 20149 after active connection goes to 20 {PID 20149} will destroy and new PID will assign. if new PID is 20159 then process between 20149 and 20159 is terminated. shows in workerman/workerman.log have a look please 1 server End stats are 2

on response of every connection i am sending 3k+ lines json. i think this is the same issue as mention above issues.

dazzlingwaqas avatar Mar 09 '19 06:03 dazzlingwaqas

2021-03-26 17:30:24 pid:195228 Worker[195228] process terminated with ERROR: E_ERROR "Allowed memory size of 134217728 bytes exhausted (tried to allocate 1056768 bytes) in /var/www/vhosts/mysite.com/httpdocs/vendor/workerman/workerman/Connection/TcpConnection.php on line 395"

seyitahmettanriver avatar Mar 26 '21 17:03 seyitahmettanriver

Hi, @walkor, does this problem solved? If the problem is solved, I am willing to donate,thank you for your selfless hard work.

lisanmiao avatar Mar 09 '23 23:03 lisanmiao

Try to call the gc_collect_cycles() gc_mem_caches() periodically.

$io->on('workerStart', function()use($io) {
    Workerman\Timer::add(60*60, function(){
        gc_collect_cycles();
        gc_mem_caches();
    });
});

I can't verify it here because I can't reproduce this problem.

walkor avatar Mar 10 '23 06:03 walkor