Bug #80158 [NEW]: Memory leak when xml nodes are stored into local array

From: Date: Sun, 27 Sep 2020 21:34:47 +0000
Subject: Bug #80158 [NEW]: Memory leak when xml nodes are stored into local array
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-229240@lists.php.net to get a copy of this message
From:             efbiaiinzinz at hotmail dot com
Operating system: Linux
PHP version:      7.4.10
Package:          SimpleXML related
Bug Type:         Bug
Bug description:Memory leak when xml nodes are stored into local array

Description:
------------
When looping over xml nodes and storing the node objects temporarily
into a local array, some memory is never released and doing it many
times in a row with different files will result in out of memory error.

Provided sample shows that ~516096 bytes is lost.
When I change the test script line
		$items[(int)(string)$item->id] = $item;
to
		$items[(int)(string)$item->id] = (string)$item->name;
or even to
		$items[(int)(string)$item->id] = $item->asXML();

Then memory amount before and after testFile() call is exactly the same.

Test script:
---------------
function generate($counter) {
	$file = tempnam(sys_get_temp_dir(), 'xml');
	$f = fopen($file, 'wb');
	fwrite($f, '<?xml version="1.0"
encoding="UTF-8"?><items>' . "\n");
	for ($i = 1; $i <= $counter; $i++)
		fwrite($f, '<item id="' . $i . '"><id>' . $i .
'</id><name>item ' . $i
. '</name></item>' . "\n");
	fwrite($f, '</items>');
	fclose($f);
	return $file;
}
function testFile($file) {
	$xml = simplexml_load_file($file);
	$items = [];
	foreach ($xml->item as $item)
		$items[(int)(string)$item->id] = $item;
}
$file = generate(50000);
$mem1 = memory_get_usage();
testFile($file);
$mem2 = memory_get_usage();
echo "$mem1\n$mem2\n";
unlink($file);

Expected result:
----------------
The test should print two exactly same numbers, separated by linebreak.

Actual result:
--------------
Second number is ~516 000 larger which means at least 0.5MB of memory
leaked somehow.

-- 
Edit bug report at https://bugs.php.net/bug.php?id=80158&edit=1
-- 
Fix committed:                    https://bugs.php.net/fix.php?id=80158&r=fixed
Fixed in release:                 https://bugs.php.net/fix.php?id=80158&r=alreadyfixed
Need backtrace:                   https://bugs.php.net/fix.php?id=80158&r=needtrace
Need Reproduce Script:            https://bugs.php.net/fix.php?id=80158&r=needscript
Try newer version:                https://bugs.php.net/fix.php?id=80158&r=oldversion
Not developer issue:              https://bugs.php.net/fix.php?id=80158&r=support
Expected behavior:                https://bugs.php.net/fix.php?id=80158&r=notwrong
Not enough info:                  https://bugs.php.net/fix.php?id=80158&r=notenoughinfo
Submitted twice:                  https://bugs.php.net/fix.php?id=80158&r=submittedtwice
register_globals:                 https://bugs.php.net/fix.php?id=80158&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=80158&r=phptooold
Daylight Savings:                 https://bugs.php.net/fix.php?id=80158&r=dst
IIS Stability:                    https://bugs.php.net/fix.php?id=80158&r=isapi
Install GNU Sed:                  https://bugs.php.net/fix.php?id=80158&r=gnused
Floating point limitations:       https://bugs.php.net/fix.php?id=80158&r=float
No Zend Extensions:               https://bugs.php.net/fix.php?id=80158&r=nozend
MySQL Configuration Error:        https://bugs.php.net/fix.php?id=80158&r=mysqlcfg


Thread (6 messages)

« previous php.bugs (#229240) next »