php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #60087 release() function and __release() method
Submitted: 2011-10-18 19:39 UTC Modified: 2018-06-24 04:22 UTC
Votes:3
Avg. Score:4.3 ± 0.9
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: luke at cywh dot com Assigned:
Status: No Feedback Package: Class/Object related
PHP Version: 5.3.8 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.
Password:
Status:
Package:
Bug Type:
Summary:
From: luke at cywh dot com
New email:
PHP Version: OS:

 

 [2011-10-18 19:39 UTC] luke at cywh dot com
Description:
------------
This is in reference to circular references, as outlined in this bug:

https://bugs.php.net/bug.php?id=33595

The "solution" in PHP 5.3 is the garbage collector, which seems to work well. 
But it's lazy, meaning it only frees memory when it has to. On a complex web 
application I came across a situation where the garbage collector didn't free 
memory like it should have. Eventually the script ran out of memory. Granted it 
got a lot farther than 5.2 did.

The most common occurrence (only?) of a circular reference is a parent and child 
relationship with objects. The unset() function is useless because a reference 
is held, so __destruct is never ran.

I propose an additional more aggressive function called release(). It would 
function just like unset(), but would additionally call a __release() method 
within the object allowing for cleanup before unsetting the object.

If the __release() method doen't exist perhaps PHP could unset the properties 
itself, and call __release() on any objects that may be stored in an 
array/property.

I've added a demo of how this could work. The demo is different in that it 
requires __release(). An internal solution may not.

Test script:
---------------
gc_disable();

interface Release {
	function __release();
}

class A implements Release {
	function __construct() {
		$this->b = new B($this);
	}

	function __release() {
		unset($this->b);
	}
}

function release(Release &$obj) {
	$obj->__release();
	$obj = NULL;
}

class B {
	function __construct($parent = NULL) {
		$this->parent = $parent;
	}
}

for($i = 0; $i < 1000000; $i++) {
    $a = new A();
    release($a);
}



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-10-18 20:08 UTC] luke at cywh dot com
Here's another demo that demonstrates the exact functionality I describe:

gc_disable();

class A {
	function __construct() {
		$this->b = new B($this);
	}

	// function __release() {
	// 	unset($this->b);
	// }
}

class B {
	function __construct($parent = NULL) {
		$this->parent = $parent;
	}
}

function release(&$var) {
	if(is_object($var)){
		if(method_exists($var, '__release')) {
			$var->__release();	
		}
		else {
			foreach(array_keys(get_object_vars($var)) as $k) {
				$v = $var->$k;
				$var->$k = NULL;
				release($v);
			}
		}
	}
	else if(is_array($var)) {
		foreach($var as &$c) {
			release($c);
		}
	}

	$obj = NULL;
}

for($i = 0; $i < 1000000; $i++) {
    $a = new A();
    release($a);
}


I realize that "array_keys(get_object_vars($var))" is horrible, especially since 
it's limited by scope. An internal version wouldn't have this sort of 
limitation.
 [2018-05-05 21:33 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2018-05-05 21:33 UTC] requinix@php.net
Does the existence of gc_collect_cycles() render this moot, given that both it and the proposed release() would require explicit function calls by a developer?
The function was added in 5.3.0 but I don't know if the reporter was aware of it.
 [2018-06-24 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 20:01:28 2024 UTC