r/PHPhelp • u/spaceemotion • Mar 05 '18
Solved Potential memory leak in PDO using 7.2.2?
I've been scratching my head trying to figure out the following issue and even opened up an official bugreport (https://bugs.php.net/bug.php?id=76053).
The following piece of code running on a brand new Laravel Homestead box using MariaDB (same deal with MySQL though) has a massive memory leak and time difference when running with 7.2 over 7.1:
<?php
$dsn = 'mysql:dbname=homestead;host=127.0.0.1';
$user = 'homestead';
$password = 'secret';
$pdo = new PDO($dsn, $user, $password);
$statement = $pdo->prepare('SELECT :column FROM :table WHERE grade <= :grade');
for($i = 0; $i < 1000; $i++) {
$statement->execute([':grade' => 100, ':column' => 'id', ':table' => 'grades']);
if ($i % 100 === 0) {
echo memory_get_usage() . "\n";
}
}The more arguments i add to the execute function, the slower it gets.
Here's the "benchmark":
vagrant@homestead:~$ time php7.2 memorytest.php
578448
632880
687280
741680
796080
850480
904880
959280
1013680
1068080
real 0m2.769s
user 0m2.676s
sys 0m0.032s
vagrant@homestead:~$ time php7.1 memorytest.php
365440
365472
365472
365472
365472
365472
365472
365472
365472
365472
real 0m0.079s
user 0m0.028s
sys 0m0.016sOn average, the execute call generates ~550bytes of garbage. I've tested it with bigger queries and it can go up to several kilobytes!
Opcache is disabled on the CLI by default, but are there any config settings I might have missed? Cheers!
1
u/MorphineAdministered Mar 06 '18
$pdo->prepare('SELECT :column FROM :table WHERE grade <= :grade');I'm surprised it works at all.
2
u/spaceemotion Mar 06 '18
It's a fairly stupid example, I admit, but even a basic insert query with no parameters at all did the same.
1
u/_odan Mar 12 '18
PHP 7.2.0, MySQL: 5.7.20, OS: Windows 7
Result:
393576
393608
393608
393608
393608
393608
393608
393608
393608
3936082
u/spaceemotion Mar 12 '18
Hey, thanks for taking the time to investigate this issue as well.
As I stated at https://www.reddit.com/r/PHPhelp/comments/8295j6/potential_memory_leak_in_pdo_using_722/dva1ci8/ already, this was an issue with the Laravel Homestead box itself, where Zend Z-Ray was also enabled for the CLI.
A PR has been made already and fixes the leak :) https://github.com/laravel/settler/pull/145
Cheers!
3
u/shaunc Mar 06 '18
I needed to do some upgrades anyway, so while I was at it, I tried and failed to reproduce this with vanilla PHP on ESX VMs. I created a table and stuffed it with 300,000 rows where grade was between 0 and 100,
I compiled PHP manually (using
--with-pdo-mysql) and didn't encounter a leak under 7.1.10, 7.2.1, 7.2.2, or 7.2.3. Testing was against MySQL 5.7.21 on CentOS 6 2.6.32-696.20.1.I wonder if it's an issue with Laravel's pre-packaged Vagrant instance?
(Note,
atl04isn't running a MySQL server; I had it remotely connecting to MySQL ondeltanso its execution times are higher.)Wherever the leak is coming from, hope you track it down!