|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #33487 Memory for object store is never released during request
Submitted: 2005-06-27 11:31 UTC Modified: 2021-11-12 13:49 UTC
Avg. Score:4.5 ± 0.8
Reproduced:54 of 55 (98.2%)
Same Version:25 (46.3%)
Same OS:30 (55.6%)
From: robert dot munteanu at betbrain dot com Assigned:
Status: Open Package: Scripting Engine problem
PHP Version: * OS: *
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: robert dot munteanu at betbrain dot com
New email:
PHP Version: OS:


 [2005-06-27 11:31 UTC] robert dot munteanu at betbrain dot com
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:
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";
echo "After: ".memory_get_usage()."\n";

Expected result:
Memory usage remains roughly the same.

Actual result:
Before: 41424
After: 432560


Add a Patch

Pull Requests

Pull requests:

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2005-06-27 12:17 UTC]
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 15:29 UTC]
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
 [2006-01-26 19:56 UTC] nick at mythgaming dot net
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):
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";
echo "One Use: ".get_usage()."\n";
echo "Two Uses: ".get_usage()."\n";

Before: 5.555MB 
One Use: 10.164MB 
Two Uses: 10.293MB
 [2010-12-20 14:14 UTC]
-Package: Feature/Change Request +Package: Scripting Engine problem -PHP Version: 6CVS-2005-06-27 +PHP Version: *
 [2014-08-26 18:55 UTC] chrisdmiddleton at gmail dot com
Has this bug been fixed in PHP 5.*?
 [2014-08-27 06:16 UTC]
This is not a bug, and it's not fixed.
 [2014-08-27 10:00 UTC]
In PHP 7 the object store is just a ptr array, so the overhead here reduces to sizeof(void *) * max_number_of_objects. This should reduce the memory usage of the object store by a factor of 8 on both 32bit and 64bit.
 [2017-10-24 08:13 UTC]
-Status: Assigned +Status: Open -Assigned To: dmitry +Assigned To:
 [2021-11-12 13:49 UTC]
-Summary: Memory allocated for objects created in object methods is not released +Summary: Memory for object store is never released during request
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Jun 24 17:01:30 2024 UTC