#33487 [Com]: Memory allocated for objects created in object methods is not released

From: Date: Thu, 26 Jan 2006 18:56:34 +0000
Subject: #33487 [Com]: Memory allocated for objects created in object methods is not released
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-92522@lists.php.net to get a copy of this message
 ID:               33487
 Comment by:       nick at mythgaming dot net
 Reported By:      robert dot munteanu at betbrain dot com
 Status:           Assigned
 Bug Type:         Scripting Engine problem
 Operating System: *
 PHP Version:      6CVS-2005-06-27
 Assigned To:      dmitry
 New Comment:

Note that the memory is freed inside php, just not yielded back to the
operating system until execution ends.

In this example, the memory is claimed as the function executes, then
re-used when the function is run again (instead of using up twice as
much memory):
----------
<?php
function doNothing() {
	$res = array();
	for ( $i = 0; $i < 20000; $i++) {
		$res[$i] = new StdClass;
	}
}

// Unix only
function get_usage() {
	$my_pid = getmypid();
	$usage = ps -eo%mem,rss,pid | grep $my_pid;
	$kb = preg_split("/\s+/", $usage);
	return sprintf("%.3f",$kb[2] / 1024) . "MB";
}

echo "Before: ".get_usage()."\n";
doNothing();
echo "One Use: ".get_usage()."\n";
doNothing();
echo "Two Uses: ".get_usage()."\n";
?>

Output
------
Before: 5.555MB 
One Use: 10.164MB 
Two Uses: 10.293MB


Previous Comments:
------------------------------------------------------------------------

[2005-11-25 01:38:09] robert dot munteanu at betbrain dot com

Problem persists in the latest PHP 5.1 release

Before: 49200
After: 440496

------------------------------------------------------------------------

[2005-09-09 18:09:50] robert dot munteanu at betbrain dot com

The problem persists on the latest stable release 5.0.5. 

Output: 
Before: 38984
After: 430144

Please let me know what the status is on fixing this bug ( or
inneficiency ).

------------------------------------------------------------------------

[2005-06-27 15:29:38] dmitry@php.net

This is not really a bug, but unefficient usage of memory by
zend_object_storage. It frees object buckets only on request shutdown.

The test case allocates:
16384 * sizeof(zend_object_store_bucket) = 393216 bytes

------------------------------------------------------------------------

[2005-06-27 12:17:20] sniper@php.net

Dmitry, is there any way to fix this..? (btw. unset() does NOT free all
the memory..but that's, AFAIK, by design)


------------------------------------------------------------------------

[2005-06-27 11:31:02] robert dot munteanu at betbrain dot com

Description:
------------
Whenever you create an object in a method, memory is not released when
the method execution ends, unset() must be called manually.

This becomes a problem when you have long-running scripts, which
perform actions repeatedly which leads to the script running out of
memory.

Reproduce code:
---------------
<?php
class Tester {
    public function doNothing() {
        $res = array();
        for ( $i = 0; $i < 10000; $i++) {
            $res[$i] = new StdClass;
        }
    }
}

$t = new Tester();
echo "Before: ".memory_get_usage()."\n";
$t->doNothing();
echo "After: ".memory_get_usage()."\n";
?>


Expected result:
----------------
Memory usage remains roughly the same.

Actual result:
--------------
Before: 41424
After: 432560



------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=33487&edit=1


Thread (10 messages)

« previous php.bugs (#92522) next »