amphp/mysql

Memory leak in \Amp\Mysql\ResultSet

demyan112rv opened this issue · 3 comments

I have a table with 100000 rows with some test data. When i try to select all rows from table i have very large the memory usage. I thought that this value will not increase depending on the number of rows. Is this expected behavior or is it a memory leak?

My example:

$db = Amp\Mysql\pool("...");

$start = microtime(true);

/** @var \Amp\Mysql\ResultSet $result */
$result = yield $db->query("SELECT * FROM `amp_memory`;");

while (yield $result->advance()) {
    // nothing
}

$end = microtime(true);

echo 'Selected 100000 rows: ' . ($end - $start) . PHP_EOL;

echo memory_get_peak_usage(true)/1024/1024 . ' MB' . PHP_EOL;

$db->close();

Output:

Selected 100000 rows: 2.8150889873505
192.00390625 MB

@bwoebi I just want to remind you about this thread 💣

It is somewhat expected - the rows are coming in much faster than we're processing them.

This isn't optimal though. There currently is no backpressure on a connection. I agree with you that this should be refactored to press back on the connection at least with resultsets. Possibly optionally.

The rows are freed as soon as they are iterated over by the way.

@bwoebi, please, see the PR above. It is hard to tell if I break anything else, but the results now as following:

Selected 100000 rows: 2.5576519966125
4 MB

Thank you in advance.