#46181 [Opn]: memory leak on garbage collecting function return values

From: Date: Fri, 26 Sep 2008 10:34:31 +0000
Subject: #46181 [Opn]: memory leak on garbage collecting function return values
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-129269@lists.php.net to get a copy of this message
 ID:               46181
 User updated by:  xl269 at cam dot ac dot uk
 Reported By:      xl269 at cam dot ac dot uk
 Status:           Open
 Bug Type:         Performance problem
 Operating System: debian gnu/linux lenny
 PHP Version:      5.3CVS-2008-09-26 (CVS)
 New Comment:

oh, i forgot to add, if you uncomment those lines in the code provided,
the bug doesn't appear. which odd, since it appears for the smaller
data.


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

[2008-09-26 10:28:47] xl269 at cam dot ac dot uk

Description:
------------
I have no idea if my explanation of this bug is correct, but here's a
guess anyway.

PHP does not seem to garbage collect return values from functions, when
they are operated-assigned (eg. $v = func() + 5; or $v = func()." ";) to
a variable in the caller. but this is fine for direct assignments (eg.
$v = func();)

It would seem from this that PHP is storing functions' return values
internally as if they were pointed to by a variable (that doesn't exist
in the script), and using copy-on-write to avoid copying this data when
it's assigned to an actual script variable. However copy-on-write,
doesn't trigger when there is an operation on this data before this
assignment.


Reproduce code:
---------------
<?php

define('LF', chr(10));

// define('REALLY_LONG_STRING', str_repeat("really ", 65536)."long
string");

function generic($for, $title) {
	mem('g+');
	$data = '<abbr title="information for
'.$for.'">'.$title.'</abbr>';
	// $data = '<abbr title="information for
'.$for.'">'.$title.'</abbr>'.REALLY_LONG_STRING;
	mem('g-');
	return $data;
}

function lol1() {
	mem('l1+');
	$data = " ".generic("name", "test")." ";
	mem('l1-');
	return $data;
}

function lol2() {
	mem('l2+');
	$data = generic("name", "test");
	mem('l2-');
	return $data;
}

echo memory_get_usage().LF;
for ($i=0; $i<8; $i++) {
	$a[] = lol2();
	echo memory_get_usage().LF;
	$a[] = lol1();
	echo memory_get_usage().LF;
}

?>


Expected result:
----------------
see below

Actual result:
--------------
635168
635240 l2+
635496 g+
635672 g-
635672 l2- (no memory leak here)
635928
636024 l1+
636072 g+
636184 g-
636248 l1- (this value should only be 2 bytes larger than the
previous)
636336



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


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



Thread (4 messages)

« previous php.bugs (#129269) next »