|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62468 SimpleXML memory leak, if circular references are removed by Garbage Collector
Submitted: 2012-07-02 20:13 UTC Modified: 2016-08-30 08:20 UTC
Avg. Score:4.0 ± 1.3
Reproduced:8 of 9 (88.9%)
Same Version:2 (25.0%)
Same OS:4 (50.0%)
From: zerkyn at gmail dot com Assigned: nikic (profile)
Status: Closed Package: SimpleXML related
PHP Version: 5.3.14 OS: Windows 7, Linux
Private report: No CVE-ID: None
 [2012-07-02 20:13 UTC] zerkyn at gmail dot com
The SimpleXMLElement memory leaks, when:
1) An object holds reference to a SimpleXMLElement, and
2) The object is involved in a circular references net, and
3) All external references to that net are cleared, so Garbage Collector finds that net, correctly sees it as a garbage cycle, and cleans all its objects

After the Garbage Collector finishes its work, the memory, consumed by SimpleXMLElement is not freed, thus producing a memory leak.

The issue is reproduced both at Windows and Linux platforms.

Full version for the test script is there:

Test script:
class SelfLinked
    public $xml;
    public $me;

    public function __construct()
        $this->xml = simplexml_load_file('pretty_big_file_of_1mb_size.xml');
        $this->me = $this;

echo "Sys memory usage before: ", SystemMemoryUsage(), "\n";

for ($i = 0; $i < 1000; $i++) {
    $a = new SelfLinked();

echo "Sys memory usage after: ", SystemMemoryUsage(), "\n";

/** ----------------------------------------------------- */
function SystemMemoryUsage() 
    // Return memory consumption by executing system tool - tasklist, ps or anything else
    // See on proper memory profiling of SimpleXML functions

Expected result:
Memory consumption does not change.

Actual result:
Huge increase of memory consumption is reported.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-07-05 11:16 UTC] Sjon at hortensius dot net
I can confirm this, but it has already been fixed in PHP 5.4. I assume this is 
because of the circular references that were fixed then.
 [2016-08-30 08:20 UTC]
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2016-08-30 08:21 UTC]
Closing per the previous comment.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Jul 24 18:01:29 2024 UTC