php - Fatal error: Allowed memory size exhausted
HI, I upload my php testing script to online vps server just now. The script used to parse a big size XML file(about 4M, 7000Lines). But my IE explorer show the online error message below.
Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 77 bytes) in /var/www/test/result/index.php on line 26
I am sure I already tested the php script on localhost successfully.
Is there any configuration need be enable/modify on my VPS? Such as php.ini or some setting for apache server? I just verified there are about 200M memory usage are avaliable for my VPS. How can I fix this?
......
function startElementHandler ($parser,$name,$attrib){
global $usercount;
global $userdata;
global $state; // Line #26;
//Debug
//print "name is: ".$name."\n";
switch ($name) {
case $name=="_ID" : {
$userdata[$usercount]["first"] = $attrib["FIRST"];
$userdata[$usercount]["last"] = $attrib["LAST"];
$userdata[$usercount]["nick"] = $attrib["NICK"];
$userdata[$usercount]["title"] = $attrib["TITLE"];
break;
}
......
default : {$state=$name;break;}
}
}
Answer
Solution:
Your PHP configuration is limiting PHP to only 16 megabytes of memory. You need to modify the
memory_limit
configuration directive in php.ini to increase it.Look for the line in php.ini that looks like this:
...and change to to a large value (
16M
= 16 megabytes, you could increase it to something like64M
for 64 megabytes, et cetera). If you can't find any line like that, add it.If you prefer to only increase it on a per-script basis, you can also use
ini_set()
to change the value for that script only.Answer
Solution:
You can also use memory_get_peak_usage() to find out which part of the program is eating your memory.
Answer
Solution:
Use a SAX parser instead of a DOM parser, and you won't run out of memory. DOM occupies 8-10 times the memory that the actual document stream does, whereas SAX has a reasonable constant overhead regardless of document size.
Answer
Solution:
please notice that some servers prevent this from working (though in a VPS this shouldn't be an issue, it's mostly a web hosting problem).
edit: instead of "
XX
" write the actual size, such as128M
Answer
Solution:
Make sure that you have specified the correct acceptable settings for:
file_uploads
upload_max_filesize
max_input_time
memory_limit
max_execution_time
post_max_size
You can call the
phpinfo()
function to find the location of yourphp.ini
file, it will also tell you the current values for the following settings that you need to modify.You could use the
ini_set
function to set some of those values from within your script too.SEE:
Howto optimize your PHP installation to handle large file uploads.
Answer
Solution:
It is dirty but in some cases with large XML files it is faster to get the values via regular expressions! And if it is a well formatted XML there should be no problem!
pseudo code:
Answer
Solution:
for shared hosting put this line to you index.php