Bug #52902 [Bgs]: Recursive References Leak Memory
Edit report at http://bugs.php.net/bug.php?id=52902&edit=1
ID: 52902
Updated by: cataphract@php.net
Reported by: jcampbell at remindermedia dot com
Summary: Recursive References Leak Memory
Status: Bogus
Type: Bug
Package: Class/Object related
Operating System: Fedora 12
PHP Version: 5.3.3
Block user comment: N
New Comment:
Your alternatives are
* recompile PHP with a smaller value of GC_ROOT_BUFFER_MAX_ENTRIES (by
default, it's relatively high: 10 000)
* call gc_collect_cycles()
* increase the memory limit
* avoid circular references
Previous Comments:
------------------------------------------------------------------------
[2010-09-21 21:57:29] jcampbell at remindermedia dot com
You're correct that it does form a cycle of memory usage going up and
then back down. However, when using cloned objects, the memory usage is
exactly the same after every loop.
So if you make the objects larger so that a bunch of them don't fit
under the memory limit but one does, then you'll run out of memory.
Again, using cloned objects, you can loop indefinitely.
Is the solution to this to manually run gc_collect_cycles() when dealing
with large objects? Because that workaround does seem to have the same
effect on memory as cloning.
------------------------------------------------------------------------
[2010-09-21 21:32:10] cataphract@php.net
How is this leaking memory? Only <2MB for 2 million objects (A+B)? An
object takes more than 1 byte...
If you make the loop instantiate 10 times more objects, you see memory
usage remains constant, so the garbage collection mechanism is
definitely working here.
------------------------------------------------------------------------
[2010-09-21 21:20:05] jcampbell at remindermedia dot com
Description:
------------
The behavior originally reported in #33595 still exists, namely,
"Objects with recursive references leak memory."
Test script:
---------------
class A {
function __construct () {
$this->b = new B($this);
}
}
class B {
function __construct ($parent = NULL) {
$this->parent = $parent;
}
}
echo memory_get_usage() . "\n";
for ($i = 0 ; $i < 1000000 ; $i++) {
$a = new A();
}
echo memory_get_usage() . "\n";
Expected result:
----------------
Memory usage should remain relatively constant. For example, if you
change the one line to:
"$this->parent = clone $parent;"
Then the output is:
632216
632392
Actual result:
--------------
Memory usage increases with each newly instantiated object.
Actual output:
631976
1756792
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=52902&edit=1
Thread (4 messages)