friends-of-reactphp / mysql

Async MySQL database client for ReactPHP.
MIT License
334 stars 66 forks source link

Fix memory leaks described in issue #179 #202

Open dmarkic opened 1 month ago

dmarkic commented 1 month ago

Hello,

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

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 commented 1 month ago

Issue #179