php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #41830 Cross referenced objects does not destroy until end of script
Submitted: 2007-06-27 20:45 UTC Modified: 2007-06-27 20:59 UTC
From: rafal dot figas at posterus dot pl Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 5.2.3 OS: Linux Gentoo
Private report: No CVE-ID: None
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: rafal dot figas at posterus dot pl
New email:
PHP Version: OS:

 

 [2007-06-27 20:45 UTC] rafal dot figas at posterus dot pl
Description:
------------
The problem is that 2 cross referenced objects are not destroyed unless the end of script. Trying to destroy them using unset() does not work.

The problem is that, if trying to analyze lots of crossed referenced objects in complex objects structure you can simply run out of the memory, and php.ini memory limits have to be very high. And I didn't find any method to reduce memory usage, to keep the script safe.

The reproduce code was tested also on older version of PHP on several physical machines.

This bug may be similar to #32403, but it's almost 2 years old, so I assume, the cause may be different.

Reproduce code:
---------------
<?
class A
{
	protected $a_napis;
	protected $b;
	
	function __construct(B $b)
	{
		$this->a_napis = str_repeat('Hello hell', 50000);
		$this->b = $b;	
	}
	
	function __destruct()
	{
		echo "Destroying A\n";
		$this->b = null;
	}
	
	function GetNapis()
	{
		return $this->a_napis;
	}
	
	function GetB()
	{
		return $this->b;
	}
}

class B
{
	protected $b_napis;
	protected $a;
	
	function __construct()
	{
		$this->b_napis = str_repeat('Hello world', 1500000);
	}
	
	function __destruct()
	{
		echo "Destroying B\n";
		$this->a = null;
	}
	
	function SetA($a)
	{
		$this->a = $a;
	}
}

echo "\nMemory usage at beginning: ".memory_get_usage(true);

$a = new A($b = new B());
$b->SetA($a);

unset($a);
unset($b);

echo "\nMemory usage at end: ".memory_get_usage(true);
echo "\n";
?>


Expected result:
----------------
Memory usage at beginning should be equal to memory at the end. Unfortunately it is not. If u comment out the line:

$b->SetA($a);

everything is ok, both memory usages are equal.

Destructors are called just before script ends - one can see messages echo()'ed from destructors.

Actual result:
--------------
The memory usages are different.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-06-27 20:59 UTC] derick@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

That\'s how PHP works (For the moment)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 18 09:01:33 2024 UTC