Skip to content

Conversation

dmarkic
Copy link

@dmarkic dmarkic commented Sep 26, 2024

Hello,

this is a memory leak fix for 0.6.x branch.

  • Executor calls removeAllListeners() on all commands.
  • Connection will remove all listeners from executor when connection closes
    • Obvious memory-leak here is when LazyConnection opens/closes connection.

Example script:

<?php

// load reactphp-mysql/vendor/autoload.php

$loop = \React\EventLoop\Loop::get();
$factory = new \React\MySQL\Factory();
$connection = $factory->createLazyConnection('localhost');
//$connection = \React\Async\await($factory->createConnection('localhost'));

$pre_memory = 0;

$loop->addPeriodicTimer(
    1,
    function () use ($connection, &$pre_memory) {
        $connection->query('SELECT \'' . str_repeat('A', 1000) . '\'')->then(
            function ($res) use (&$pre_memory) {
                echo " - query done\n";
                $cur_memory = memory_get_usage();
                $new_memory = $cur_memory - $pre_memory;
                echo " - Memory usage: " . memory_get_usage() . " new: " . $new_memory . "\n";
                $pre_memory = $cur_memory;
            },
            function ($e) {
                echo " - query error: " . $e->getMessage() . "\n";
                throw $e;
            }
        );
    }
);

$loop->run();

@dmarkic
Copy link
Author

dmarkic commented Sep 26, 2024

Issue #179

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant