php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54120 Impossible to freed memory complety after call to Xpath() method.
Submitted: 2011-02-28 15:58 UTC Modified: 2011-03-01 22:15 UTC
From: ikaos at gmx dot fr Assigned:
Status: Not a bug Package: SimpleXML related
PHP Version: 5.3.5 OS: Linux CentOS 5.5 x86_64
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: ikaos at gmx dot fr
New email:
PHP Version: OS:

 

 [2011-02-28 15:58 UTC] ikaos at gmx dot fr
Description:
------------
---
From manual page: http://www.php.net/simplexmlelement.xpath
---


Test script:
---------------
<?php
$sxe = NULL; $nodes = NULL;
print("Without call to xpath method : " . PHP_EOL);
print("Memory before SXE  : " . memory_get_usage() . PHP_EOL);
$sxe = new SimpleXMLElement("large_file.xml", 0, TRUE);
$sxe = NULL; $nodes = NULL;
print("Memory after freed memory : " . memory_get_usage() . PHP_EOL . PHP_EOL);

$sxe = NULL; $nodes = NULL;
print("Calling xpath method : " . PHP_EOL);
print("Memory before SXE  : " . memory_get_usage() . PHP_EOL);
$sxe = new SimpleXMLElement("large_file.xml", 0, TRUE);
$nodes = $sxe->xpath('/ITEMS/ITEM');
$sxe = NULL; $nodes = NULL;
print("Memory after freed memory : " . memory_get_usage() . PHP_EOL . PHP_EOL);

// short sleep, time to launch 'ps aux'
sleep(10);
?>


Expected result:
----------------
When I unset (or set to NULL) the result of the Xpath() method ($nodes in the example), I expect memory will be completly freed. It's not the case. 

I tried, to recursively unset each element from $nodes and I got the same result. 



Actual result:
--------------
Result of the test script :

Without call to xpath method : 
Memory before SXE  : 625736
Memory after freed memory : 625904

Calling xpath method : 
Memory before SXE  : 625904
Memory after freed memory : 1608944


memory_get_usage says that few kilos octets of memory are still used, but in reality several thousand mega octets are still used.

# ps aux | grep test.php

me  28859 83.4 48.3 *1119908* *996056* pts/3  S+   15:30   0:08 /bin/php test.php

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-03-01 20:54 UTC] rrichards@php.net
-Status: Open +Status: Bogus
 [2011-03-01 20:54 UTC] rrichards@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Memory is freed. http://xmlsoft.org/xmlmem.html you can read how memory is managed 
and might not be returned immediately to the kernel.
 [2011-03-01 22:15 UTC] ikaos at gmx dot fr
Thank you for this quick answer. I red these informations on http://xmlsoft.org/xmlmem.html before.

But there is something strange (perhaps I don't understand how kernel release memory), after freeing memory I could understand the kernel doesn't release immediatly the memory, but in that case there is a big problem for me, for exemple I use pcntl and try to fork my script after using xpath and releasing memory, and it fails. If I don't use xpath() it works.

I think I understand there is a command malloc_trim, but it doesn't exists directly from PHP. Is there a workaround I could use ?

Second question, It seems that memory_get_usage returns the memory used by PHP, after freeing memory (after xpath()) I still have more memory used than before using xpath(). Is it normal or still related to the way the kernel works ?

Thanks.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 14:01:32 2024 UTC