php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52959 PDO_MYSQL: Memory leak when unsetting PDOStatement and/or resultset
Submitted: 2010-09-29 23:45 UTC Modified: 2014-01-01 12:47 UTC
Votes:4
Avg. Score:4.0 ± 1.0
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (50.0%)
From: eddawley at gmail dot com Assigned:
Status: Open Package: PDO MySQL
PHP Version: 5.3.3 OS: Centos 5
Private report: No CVE-ID:
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: eddawley at gmail dot com
New email:
PHP Version: OS:

 

 [2010-09-29 23:45 UTC] eddawley at gmail dot com
Description:
------------
I am noticing unexpected, inconsistent memory use when unsetting the resultset 
of a PDOStatement and/or unsetting the statement itself.

I have attached a quick test script to illustrate the problematic behavior.  I 
am selecting approximately 500k rows in order to make the memory usage obvious.  
The script should be run with all of the unsets commented out and then with some 
or all uncommented.

Run with:

no unsets: system memory usage fluctuates but relatively stable, peak memory 
usage increases

uncommenting some or all of the unsets: system and peak usage will continually 
increase




I understand how the memory usage could fluctuate due to garbage collection but 
I do not understand why the addition of unsets would cause a steady increase in 
memory usage.  This appears to be a memory leak.

Also, top/ps output for RSS/VSZ of the process matches the "peak" memory usage 
and not the system allocated or php allocated numbers.  Is this to be expected?

Test script:
---------------
<?php

$db = connect();

while(1){
    echo(memory_get_usage(true) . "\t" . memory_get_usage() . "\t" . memory_get_peak_usage() . "\n");
    blah($db);
}


function blah($db){
    $sql = "select id from table LIMIT 500000";
    $stmt = $db->prepare($sql);

    $stmt->execute();
    $a = $stmt->fetchAll();
    //unset($a);

    $stmt->execute();
    $a = $stmt->fetchAll();
    //unset($a);

    //unset($stmt);
}

function connect(){
    $dsn = 'mysql:host=127.0.0.1;dbname=db;port=3306';
    $options = array(PDO::ATTR_TIMEOUT => APPLICATION_DB_PDO_TIMEOUT);
    $db = new PDO($dsn, 'user', 'pass', $options);
    return $db;
}


Expected result:
----------------
Memory usage over time should be relatively stable overall.  Peak memory usage 
should be lowest when unsetting the first resultset.

Actual result:
--------------
Memory usage continues to increase over time if any of the unsets are uncommented.  
Peak memory usage is halved when the first resultset is unset but it continues to 
increase over time.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-06-12 04:18 UTC] ssufficool@php.net
-Summary: Memory leak when unsetting PDOStatement and/or resultset +Summary: PDO_MYSQL: Memory leak when unsetting PDOStatement and/or resultset
 [2014-01-01 12:47 UTC] felipe@php.net
-Package: PDO related +Package: PDO MySQL
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Tue Aug 29 15:01:52 2017 UTC