php-src
php-src copied to clipboard
leak in PDO and mysqli
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.

PHP Version
PHP 8.0.12
Operating System
Ubuntu 20.04
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.
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