Bug #80158 [Opn->Fbk]: Memory leak when xml nodes are stored into local array
| From: | nikic@php.net | Date: | Thu, 01 Oct 2020 14:50:01 +0000 |
| Subject: | Bug #80158 [Opn->Fbk]: Memory leak when xml nodes are stored into local array | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-229310@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=80158&edit=1
ID: 80158
Updated by: nikic@php.net
Reported by: efbiaiinzinz at hotmail dot com
Summary: Memory leak when xml nodes are stored into local
array
-Status: Open
+Status: Feedback
Type: Bug
Package: SimpleXML related
Operating System: Linux
PHP Version: 7.4.10
Block user comment: N
Private report: N
New Comment:
> 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.
From a cursory look, this looks like an object store "leak", which means that memory usage
is proportional to the maximum number of objects live at the same time. This is generally harmless
as it does not result in a persistent leak and the memory is reused when creating new objects.
However, it also means that "doing it many times in a row with different files will result in
out of memory error" does not hold, assuming you don't keep objects alive between files.
Previous Comments:
------------------------------------------------------------------------
[2020-09-27 21:34:47] efbiaiinzinz at hotmail dot com
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 this bug report at https://bugs.php.net/bug.php?id=80158&edit=1