php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66939 memory leak when assigning big arrays of objects to variable while iterating
Submitted: 2014-03-21 20:07 UTC Modified: 2016-12-17 15:29 UTC
Votes:6
Avg. Score:4.3 ± 1.1
Reproduced:6 of 6 (100.0%)
Same Version:2 (33.3%)
Same OS:1 (16.7%)
From: anton dot ponadiozin at gmail dot com Assigned: nikic (profile)
Status: Closed Package: Performance problem
PHP Version: 5.4.26 OS: Debian Wheezy
Private report: No CVE-ID: None
 [2014-03-21 20:07 UTC] anton dot ponadiozin at gmail dot com
Description:
------------
Memory consumption increases while assigning big (>1000) arrays of object to same variable. 
In my case i was generating some financial report for long period of time. Database were returning array of result objects. Longer period i was selecting significantly more memory (than it should be) was used.


➜  ~  uname -a
Linux ap 3.12-0.bpo.1-amd64 #1 SMP Debian 3.12.9-1~bpo70+1 (2014-02-07) x86_64 GNU/Linux

➜  ~  php -v
PHP 5.4.4-14+deb7u8 (cli) (built: Feb 17 2014 09:18:47) 
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies
    with Xdebug v2.2.1, Copyright (c) 2002-2012, by Derick Rethans


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

function get_objects() {
    $array = array();

    for ($i=0; $i<5000; $i++) {
        $object = new stdClass();
        $object->a = rand();
        $object->b = rand();
        $object->c = rand();
        $object->d = rand();
        $object->e = rand();

        $array[] = $object;
    }

    return $array;
}

$results = array();

for ($i=0; $i<30; $i++) {
    $results = get_objects();
    echo memory_get_usage(true) . "\n";

    // assigning empty array fixes the problem
    // $results = array();
}


Expected result:
----------------
➜  ~  php bug.php
6291456
6291456
6291456
6291456
6291456
6291456
6291456
6291456
6291456
6291456
6291456
6291456
6291456
6291456
6291456
6291456
6291456
6291456
6291456
6291456
6291456
6291456
6291456
6291456
6291456
6291456
6291456
6291456
6291456
6291456


Actual result:
--------------
➜  ~  php bug.php
6291456
7340032
7340032
7602176
7602176
7864320
7864320
8126464
8126464
8388608
8388608
8650752
8650752
8912896
8912896
9175040
9175040
9437184
9437184
9699328
9699328
9961472
9961472
10223616
10223616
10485760
10485760
10747904
10747904
11010048


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-03-21 20:14 UTC] anton dot ponadiozin at gmail dot com
Just upgraded to latest PHP, the problem persists

➜  ~  php -v
PHP 5.4.26-1~dotdeb.1 (cli) (built: Mar  7 2014 09:42:03)
 [2014-08-15 04:29 UTC] klakurka at gmail dot com
Issue is still present in PHP 5.5.15. Usage continues to rise by an ever-increasing amount -- this is not linear.

Code Snippet:
---

foreach ($things as $thing) {
	if(date('g:iA', $thing['intTimeStamp']) == '8:00AM') {
		echo 'Starting new day... ', date('F jS, Y', $thing['item']), '<br />';
                echo "Total: " . memory_get_peak_usage()/1024/1024 . "MB<br />";
	}
	ob_flush();
	flush();
        $startMemory = memory_get_peak_usage()/1024/1024;
        $largeDataSet = $api->getLargeDataSet($thing['something']);
        echo 'Memory Delta: ' . (memory_get_peak_usage()/1024/1024 - $startMemory) . '<br />';
        $dataProcessor->processData($thing['something'], $largeDataSet);
}

---

Linux server.localdomain 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

PHP 5.5.15 (cli) (built: Jul 24 2014 10:25:49)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
    with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2014, by Zend Technologies
 [2015-05-18 02:31 UTC] cmb@php.net
This issue is fixed in master (PHP 7), see <http://3v4l.org/5q6ut>.
 [2016-12-17 15:29 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2016-12-17 15:29 UTC] nikic@php.net
Closing here, because
 * the problem has been fixed in PHP 7.
 * this is not technically a leak, but rather a memory fragmentation issue, as seen if you use memory_get_usage(false). If you perform more runs, the memory usage converges to twice the minimally necessary memory.
 * active support for PHP 5.6 is ending, and we're not going to do non-trivial changes to the MM at this point.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 06:01:29 2024 UTC