php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75402 Possible Memory Leak using PDO::CURSOR_SCROLL option
Submitted: 2017-10-17 21:33 UTC Modified: -
From: olric86 at gmail dot com Assigned:
Status: Closed Package: PDO PgSQL
PHP Version: 7.1.10 OS: Windows 10
Private report: No CVE-ID: None
 [2017-10-17 21:33 UTC] olric86 at gmail dot com
Description:
------------
Hi, 

while migrating a project which was using PDO->MSSQL (https://docs.microsoft.com/it-it/sql/connect/php/microsoft-php-driver-for-sql-server) to PDO->PostgreSQL, I found a strange behavior in how memory is retained by PHP when prepared statements are 'prepared' with the

\PDO::ATTR_CURSOR => \PDO::CURSOR_SCROLL

option, at least on Microsoft Windows.
By using the test script attached, it looks like memory used during the result set object retrieval is never de-allocated, leading to an abnormal memory consumption and ultimately to a httpd.exe crash on 32 bit systems due to memory allocation limitations.

I'm using a x64 build of PHP 7.1.10 with Apache 2.4.28 on a Windows 10 x64 PRO and I've also tested the following with PHP 5.4 32 bit and with PHP 7.0 32bit.
The behavior looks to be the same, each time the script is ran the memory used by httpd.exe increments by almost 800KB and it never get released. 
We saw processes keeping up to 1.5Gb of RAM on 32bit OSs.

By simply changing the prepared statement 'prepare' option to:

\PDO::ATTR_CURSOR => \PDO::CURSOR_FWDONLY

memory is allocated and de-allocated correctly.

You can see here:

https://drive.google.com/open?id=0Bx83lRq7kAiKY0ViRFJWMkxBZlE

how quickly memory is being allocated and never released. 
Initially I thought it was some kind of caching mechanism of PHP/PDO/PGSQL but memory allocation seems to be simply increasing with no limit.

This does not happen with the CURSOR_FWDONLY option.

Please find a sample test script below.


Test script:
---------------
SQL Table creation script:

https://drive.google.com/open?id=0Bx83lRq7kAiKSldZNEZ5QXh5R0E

SQL Dummy data insert script:

https://drive.google.com/open?id=0Bx83lRq7kAiKdTJRTERjQzh1NEk

PHP test script:

https://drive.google.com/open?id=0Bx83lRq7kAiKQWt5QTNHdmhPVmc



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-07-30 16:30 UTC] ab@php.net
Automatic comment on behalf of ab
Revision: http://git.php.net/?p=php-src.git;a=commit;h=cc69950b1527b321bf54a9394938d57e8f5a35cc
Log: Fixed bug #75402 Possible Memory Leak using PDO::CURSOR_SCROLL option
 [2018-07-30 16:30 UTC] ab@php.net
-Status: Open +Status: Closed
 
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Wed Sep 26 10:01:25 2018 UTC