|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 15:00:01 2025 UTC |
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