php-src icon indicating copy to clipboard operation
php-src copied to clipboard

leak in PDO and mysqli

Open Yurunsoft opened this issue 4 years ago • 2 comments

Description

The following code:

<?php

declare(strict_types=1);

function leakPDO()
{
    $pdo = new PDO('mysql:host=127.0.0.1', 'root', 'root');
    for ($i = 0; $i < 1000; ++$i)
    {
        $pdo->query('select ' . mt_rand());
    }
}

function leakMysqli()
{
    $mysqli = new mysqli('127.0.0.1', 'root', 'root');
    for ($i = 0; $i < 1000; ++$i)
    {
        $mysqli->query('select ' . mt_rand());
    }
    $mysqli->close();
}

while (true)
{
    leakPDO();
    // leakMysqli();

    echo 'input:',\PHP_EOL;
    fread(\STDIN, 1);
}

Every time you press enter, php uses more memory.

I think this problem also exists in PHP 7.4, 8.1.

I'm using php to develop a memory resident application and this problem is bothering me.

1 2 3

PHP Version

PHP 8.0.12

Operating System

Ubuntu 20.04

Yurunsoft avatar Feb 13 '22 02:02 Yurunsoft

I believe this is caused by interned strings. Replace random column names with real names that don't change that often and PHP won't allocate that much memory.

kamil-tekiela avatar Feb 13 '22 09:02 kamil-tekiela

I believe this is caused by interned strings. Replace random column names with real names that don't change that often and PHP won't allocate that much memory.

Thanks

Yurunsoft avatar Feb 13 '22 09:02 Yurunsoft