php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62242 Memory Leak in xml_parser_create_ns()
Submitted: 2012-06-06 10:14 UTC Modified: 2018-04-01 15:58 UTC
Votes:5
Avg. Score:4.2 ± 1.0
Reproduced:3 of 3 (100.0%)
Same Version:1 (33.3%)
Same OS:1 (33.3%)
From: martinhv at arcor dot de Assigned: cmb (profile)
Status: Closed Package: XML related
PHP Version: 5.4Git-2012-06-06 (snap) OS: Debian wheezy/sid
Private report: No CVE-ID: None
 [2012-06-06 10:14 UTC] martinhv at arcor dot de
Description:
------------
The function xml_parser_create_ns() use memory that doesn't freed after xml_parser_free() is called. This memory isn't registered by php and the function memory_get_usage() doesn't report it. But you can see the eating memory via the ps command. (See the actual result below)

If the function xml_parser_create() is used, the used memory stagnates to a constant value. So no memory leak exists. (See the expected result below)

If no xml_set_element_handler() is called, there isn't a memory leak, too.

So only the combination of xml_parser_create_ns() with xml_set_element_handler() produced a memory leak!

I tested with the PHP snapshot 5.4 from 2012-06-05 on Debian Wheezy Alpha1 with libxml2 version 2.7.8.dfsg-9.1. But many other PHP versions reproduce the bug, too.

Test script:
---------------
#!/usr/local/bin/php
<?php

$xml='<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root xmlns="http://test/ns">
	<test name="Name" value="Value" key="Key" id="Id" test="Test"/>
	<test name="Name" value="Value" key="Key" id="Id" test="Test"/>
	<test name="Name" value="Value" key="Key" id="Id" test="Test"/>
</root>';

for ($i=0; $i<10000; $i++) {
	$xml_parser = xml_parser_create_ns('utf-8', '@');
	xml_set_element_handler($xml_parser, 'start_tag_handler', 'end_tag_handler');
	xml_parse($xml_parser, $xml, true);
	xml_parser_free($xml_parser);
	$xml_parser = NULL;

	if (($i % 1000) == 0)
		echo	"memory_get_usage(): ".memory_get_usage(true)." bytes\n".
			"real memory size:   ".(exec("ps h -C test2.php -o rsz")*1024)." bytes\n";
}

function start_tag_handler($parser, $name, $attribs) {
}

function end_tag_handler($parser, $name) {
}
?>

Expected result:
----------------
memory_get_usage(): 262144 bytes
real memory size:   3743744 bytes
memory_get_usage(): 262144 bytes
real memory size:   3743744 bytes
memory_get_usage(): 262144 bytes
real memory size:   3796992 bytes
memory_get_usage(): 262144 bytes
real memory size:   3796992 bytes
memory_get_usage(): 262144 bytes
real memory size:   3796992 bytes
memory_get_usage(): 262144 bytes
real memory size:   3796992 bytes
memory_get_usage(): 262144 bytes
real memory size:   3796992 bytes
memory_get_usage(): 262144 bytes
real memory size:   3796992 bytes
memory_get_usage(): 262144 bytes
real memory size:   3796992 bytes
memory_get_usage(): 262144 bytes
real memory size:   3796992 bytes

Actual result:
--------------
memory_get_usage(): 262144 bytes
real memory size:   3772416 bytes
memory_get_usage(): 262144 bytes
real memory size:   4222976 bytes
memory_get_usage(): 262144 bytes
real memory size:   4657152 bytes
memory_get_usage(): 262144 bytes
real memory size:   5091328 bytes
memory_get_usage(): 262144 bytes
real memory size:   5750784 bytes
memory_get_usage(): 262144 bytes
real memory size:   6225920 bytes
memory_get_usage(): 262144 bytes
real memory size:   6656000 bytes
memory_get_usage(): 262144 bytes
real memory size:   7090176 bytes
memory_get_usage(): 262144 bytes
real memory size:   7524352 bytes
memory_get_usage(): 262144 bytes
real memory size:   7958528 bytes

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-04-06 09:45 UTC] jbr at emperion dot net
As per April 2016 the memory leak on Ubuntu 14:04 with PHP 5.5.9 still exists. Using JAXL xmpp package, this means that I have to restart the PHP daemon every few hours, which is a shame. SO I would be very happy if the memory leak could be fixed or if someone know how to avoid it.
 [2018-04-01 15:58 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2018-04-01 15:59 UTC] cmb@php.net
I cannot reproduce the reported memory leaks with PHP 7.1, which
is the oldest branch wich is still actively supported[1].  I
suppose the issue has been fixed in the meantime, and close this
ticket.  Please re-open if the memory leak still occurs for you.

[1] <http://www.php.net/supported-versions.php>
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Mon Jul 06 02:01:28 2020 UTC