r/PHPhelp • • 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.016s

On 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!

8 Upvotes

7 comments sorted by

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,

CREATE TABLE `reddit_8295j6` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `grade` int(11) NOT NULL,
  PRIMARY KEY (`id`,`grade`)
) ENGINE=InnoDB AUTO_INCREMENT=300001 DEFAULT CHARSET=latin1

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, atl04 isn't running a MySQL server; I had it remotely connecting to MySQL on deltan so its execution times are higher.)

[user@atl04 /tmp]$ php -v
PHP 7.1.10 (cli) (built: Nov 22 2017 14:02:13) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
[user@atl04 /tmp]$ time php pdotest.php
363448
363480
363480
363480
363480
363480
363480
363480
363480
363480

real    0m49.894s
user    0m0.272s
sys     0m0.248s

[user@deltan ~/temp]$ php -v
PHP 7.2.1 (cli) (built: Jan  9 2018 21:15:35) ( ZTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2017 Zend Technologies
[user@deltan ~/temp]$ time php pdotest.php
405656
405688
405688
405688
405688
405688
405688
405688
405688
405688

real    0m0.121s
user    0m0.053s
sys     0m0.026s

[user@atl04 /tmp]$ php -v
PHP 7.2.2 (cli) (built: Mar  5 2018 22:01:21) ( ZTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
[user@atl04 /tmp]$ time php pdotest.php
400136
400168
400168
400168
400168
400168
400168
400168
400168
400168

real    0m49.772s
user    0m0.237s
sys     0m0.254s

[user@deltan ~/temp]$ php -v
PHP 7.2.3 (cli) (built: Mar  5 2018 22:05:43) ( ZTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
[user@deltan ~/temp]$ time php pdotest.php
405656
405688
405688
405688
405688
405688
405688
405688
405688
405688

real    0m0.098s
user    0m0.040s
sys     0m0.026s

Wherever the leak is coming from, hope you track it down!

3

u/spaceemotion Mar 06 '18

Hey, thanks for taking the time for this in-depth analysis! I've actually tried the same on a cheap cloud instance (think digital ocean) and had the same experience. I'll try to compare the config used by the laravel box as it seems to be more like a configuration/plugin issue at this point.

3

u/spaceemotion Mar 06 '18

Alright, so @fitztrev over at the larachat slack channel has figured out that it was actually the Zend Z-Ray extension accidentally being installed for the CLI as well. Removing the associated config file that enables the extension fixed all of our issues.

The only question I'd have is why the problem does not happen when the PHP-FPM package uses it. but for now; case closed!

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
393608

2

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!