php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45291 Memory failure in very large XML file
Submitted: 2008-06-17 08:04 UTC Modified: 2008-09-18 12:16 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: gilbert dot musnik at fr dot adp dot com Assigned:
Status: Not a bug Package: SimpleXML related
PHP Version: 5.2.6 OS: Windows
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: gilbert dot musnik at fr dot adp dot com
New email:
PHP Version: OS:

 

 [2008-06-17 08:04 UTC] gilbert dot musnik at fr dot adp dot com
Description:
------------
Hello, 

It's been days I am trying to sort out a problem which pops up when I process a huge XML file.  In "reproduce code" windows is a trivial script which allocates memory by making an array very big.  Assuming a memory_limit of 1024M in <php.ini>, if I don't load the huge XML file, the script crashes as expected with a "memory exhausted" fatal error diagnostic (see expected results window).

However, if I do load my huge XML file with simplexml_load_file(around 300 MB!), the script crashes with a "Out of memory" fatal error diagnostic.  By the way, it doesn't matter if memory_limit is set to 512M or 2048M, the script stumbles on the same "allocated" figure of 332 MB (see actual results windows).

The strangest thing is that before I start swelling the array, the memory usage is almost the same whether ot not simplexml_load_file("VERY_BIG_FILE.XML") is invoked or not.

It looks as if memory allocated by simplexml_load_file() routine is NOT taken into account in overall PHP memory usage and memory limit management.

Thanks in advance for your feedback on that,

Gilbert Musnik


Reproduce code:
---------------
<?php

$flag_xml = true;
$now = Date("Y/m/d - H:i:s");
if ($flag_xml) {
	print ("[ $now ] Before simplexml_load_file\n");
	$f_inp = "VERY_BIG_FILE.xml";  // mine is 300 MB
	
	$ll0 = time();
	$xml = simplexml_load_file ($f_inp);
	$ll1 = time() - $ll0;
	$now = Date("Y/m/D - H:i:s");
	print ("[ $now ] After  simplexml_load_file: $ll1 secondes\n");
	}
else {
	print ("[ $now ] NO simplexml_load_file\n");
	}

$bufrs = array();
$nmax = 150000000;
for ($ii = 0; $ii < $nmax; $ii ++) {
	$bufr = str_repeat ("ABCDEFGHIJ", 1);
	array_push ($bufrs, $bufr);
	$jj = $ii % 1000000;
	if ($jj == 0) {
		$xx = number_format ($ii);
		$qq = number_format (memory_get_usage());
		$memory_limit = ini_get('memory_limit');
		print ("$xx - memory usage=$qq  -  memory_limit=$memory_limit)\n");
		}
	};
?>


Expected result:
----------------
N:\>\php-5.2.6-Win32\php.exe -c \MD_config\php.ini \MD_config\test_mem.php 
[ 2008/06/16 - 18:06:10 ] No simplexml_load_file
0 - memory usage=64,552  -  memory_limit=1024M)
1,000,000 - memory usage=100,259,664  -  memory_limit=1024M)
2,000,000 - memory usage=200,454,016  -  memory_limit=1024M)
3,000,000 - memory usage=304,842,624  -  memory_limit=1024M)
4,000,000 - memory usage=400,842,624  -  memory_limit=1024M)
5,000,000 - memory usage=513,619,840  -  memory_limit=1024M)
6,000,000 - memory usage=609,619,840  -  memory_limit=1024M)
7,000,000 - memory usage=705,619,840  -  memory_limit=1024M)
8,000,000 - memory usage=801,619,840  -  memory_limit=1024M)
9,000,000 - memory usage=931,174,272  -  memory_limit=1024M)
10,000,000 - memory usage=1,027,174,272  -  memory_limit=1024M)

Fatal error: Allowed memory size of 1073741824 bytes exhausted (tried to allocate 35 bytes) in N:\md_config\test_mem.php on line 23

Actual result:
--------------
N:\>\php-5.2.6-Win32\php.exe -c \MD_config\php.ini \MD_config\test_mem.php 
[ 2008/06/16 - 18:05:06 ] Before simplexml_load_file
[ 2008/06/Mon - 18:05:33 ] After  simplexml_load_file: 27 secondes
0 - memory usage=65,472  -  memory_limit=1024M)
1,000,000 - memory usage=100,260,376  -  memory_limit=1024M)
2,000,000 - memory usage=200,454,728  -  memory_limit=1024M)
3,000,000 - memory usage=304,843,336  -  memory_limit=1024M)

Fatal error: Out of memory (allocated 332660736) (tried to allocate 35 bytes) in N:\md_config\test_mem.php on line 23




Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-09-18 12:16 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 for xml is allocated by third party library and not part of PHP 
memory usage controls. If you don't know/can't control size of the xml 
files, you should use streaming access (xmlreader or ext/xml) and not 
dom based (simplexml or dom).
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Feb 05 08:01:30 2025 UTC