DiscordPHP-Http icon indicating copy to clipboard operation
DiscordPHP-Http copied to clipboard

Memory leak

Open Exanlv opened this issue 2 years ago • 4 comments

Noticed a minor memory leak in my project, after some debugging I was able to trace it back to DiscordPHP-Http

Executing API calls keeps taking up more memory. It doesn't appear to plateau at some point.

I verified this with the following code

<?php

use ByteUnits\Metric;
use Discord\Http\Drivers\React;
use Discord\Http\Endpoint;
use Discord\Http\Http;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use React\EventLoop\Loop;

require './vendor/autoload.php';

$log = new Logger('stability-bot');
$log->pushHandler(new StreamHandler('dhttp.log'));


$http = new Http(
    'Bot <TOKEN>',
    Loop::get(),
    $log,
    new React(
        Loop::get(),
    )
);

Loop::get()->addPeriodicTimer(3, function () use ($http) {
    $http->post(
        Endpoint::bind(
            Endpoint::CHANNEL_MESSAGES,
            '577119668448526359'
        ),
        [
            'content' =>
                'Memory usage: '
                    . Metric::bytes(memory_get_usage())->format()
        ]
    );
});
{
    "require": {
        "discord-php/http": "^10.1",
        "monolog/monolog": "^3.3",
        "gabrielelana/byte-units": "^0.5.0"
    }
}

It starts out like this:

Memory usage: 590.26kB
Memory usage: 937.66kB
Memory usage: 940.10kB
Memory usage: 942.45kB

It has an initial hike after the first message, probably because it creates a bucket for this request.

After that it consistently creeps up, ended up stopping it after about 20-30 minutes

Memory usage: 2.75MB
Memory usage: 2.76MB
Memory usage: 2.76MB
Memory usage: 2.76MB
Memory usage: 2.76MB
Memory usage: 2.77MB

Profiling output: image Won't attach the complete profile as this may also contain information like my discord token

Exanlv avatar Feb 27 '23 16:02 Exanlv

Re-tested with Guzzle driver instead of React, problem persists.

Exanlv avatar Mar 01 '23 12:03 Exanlv

Retested some more, issues not present when using React HTTP or Guzzle HTTP without this lib memleak-testing.zip

Exanlv avatar Mar 01 '23 15:03 Exanlv

https://github.com/discord-php/DiscordPHP-Http/pull/20

@Exanlv I believe I found the issue.

ellisonpatterson avatar Apr 20 '23 18:04 ellisonpatterson

#20

@Exanlv I believe I found the issue.

Thank you, that is one of it, but there's probably more so issue still open

SQKo avatar Apr 21 '23 13:04 SQKo