php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52487 PDO::FETCH_INTO leaks memory
Submitted: 2010-07-29 15:47 UTC Modified: 2010-07-30 01:39 UTC
From: marques at displague dot com Assigned: felipe (profile)
Status: Closed Package: PDO related
PHP Version: 5.3.3 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: marques at displague dot com
New email:
PHP Version: OS:

 

 [2010-07-29 15:47 UTC] marques at displague dot com
Description:
------------
When assigning an object to a PDO statement via FETCH_INTO a memory leak occurs 
that can not be corrected through php code.  A subsequent FETCH_INTO with a 
different object only furthers the problem.  Changing the fetch mode, unsetting 
the statement, closing the cursor, and calling the garbage collection do not free 
the memory either. 

This bug exists in 5.2 and 5.3.

Test script:
---------------
<?php
if (! extension_loaded('pdo_sqlite')) {
	dl('pdo_sqlite.so');
}
$pdo = new PDO('sqlite::memory:');
echo "Start mem:".memory_get_peak_usage()."\n";

// Simple test
$stmt = $pdo->prepare("select 1 as attr");
for($i=0; $i<100000; $i++) {
	$stmt->setFetchMode(PDO::FETCH_INTO, new StdClass);
	echo "mem: ".memory_get_usage()."\n";

}
// End - Simple Test

// Alternate test with failed remedies
class test {
	public $attr;
	public function load($pdo) {
		$stmt = $pdo->prepare("select 1 as attr");
		//$stmt->setFetchMode(PDO::FETCH_INTO, $this); // This leaks quickly
		$stmt->setFetchMode(PDO::FETCH_INTO, new StdClass); // This still leaks
		$stmt->setFetchMode(PDO::FETCH_ASSOC); // You might think this would fix the leak - no effect
		$stmt->closeCursor(); // No effect
		unset($stmt); // No effect
	}
}

for($i=0; $i<100000; $i++) {
	$test = new test;
	$test->load($pdo);
	if (function_exists('gc_collect_cycles')) {
		echo "+gc+ ";
		gc_collect_cycles(); // No effect
	}
	unset($test);
	echo "mem: ".memory_get_peak_usage()."\n";
}



Expected result:
----------------
I would expect the peak memory usage to remain consisten on subsequent iterations.

Actual result:
--------------
The peak memory usage continues to rise until the php memory limit is hit.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-07-30 01:38 UTC] felipe@php.net
Automatic comment from SVN on behalf of felipe
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=301706
Log: - Fixed bug #52487 (PDO::FETCH_INTO leaks memory)
 [2010-07-30 01:39 UTC] felipe@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: felipe
 [2010-07-30 01:39 UTC] felipe@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Mon Nov 29 12:03:14 2021 UTC