NewEXE
NewEXE

Reputation: 81

PDO/mysqli memory leak on multiple SELECT queries

I have a memory leak when executing many (20,000) simple SELECT operations using PDO or mysqli.

// test2.php
$i = 0;
while (true) {

    $pdo->query("SELECT 1 as m");
    file_put_contents(__FILE__ . '.log', 'Memory: ' . memory_get_usage_in_mb() . PHP_EOL, FILE_APPEND);
    // In test2.php.log
    // ([line]: [message]):
    // 1:     Memory: 0.39
    // 5000:  Memory: 0.44
    // 10000: Memory: 0.51
    // 20000: Memory: 0.63

    if ($i === 20000) {
        break;
    }
    $i++;
}

Plese see full test code at https://gist.github.com/NewEXE/ca4f5ddbeb7ff863b8c775c238698c57

I also tried PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false with $pdo::closeCursor call after each query and this code on mysqli case:

$result = $mysqli->query("SELECT 1 as m", MYSQLI_USE_RESULT); // and MYSQLI_STORE_RESULT too...

// with and without this lines:
$result->free_result();
$result = null;
unset($result);

Sorry for my english and thank you in advance!

Upvotes: 1

Views: 999

Answers (2)

NewEXE
NewEXE

Reputation: 81

I disabled xDebug and the problem is solved!

Tnanks to this answer: https://stackoverflow.com/a/43359644/8927920

Upvotes: 1

Ahmed Abdelazim
Ahmed Abdelazim

Reputation: 741

immediately After

$pdo = new \PDO($dsn, $dbParams['username'], $dbParams['password']);

add this

$pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);

test on both files

Upvotes: 1

Related Questions