views:

180

answers:

6

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;}
    }
}
+2  A: 

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:

memory_limit = 16M

...and change to to a large value (16M = 16 megabytes, you could increase it to something like 64M 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.

Amber
I think it would be better to set it larger only for this file, but leave the lower value for default config.
Lo'oris
Nano HE
+1  A: 
ini_set("memory_limit","XXM");

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 as 128M

Lo'oris
+1 If you can then use this just for the code that needs the extra memory rather than making it global.
Colonel Sponsz
@Lo'oris, I tried to inserted `ini_set("memory_limit","128M");` to my index.php file. But it still can't work. showed the same error message. I guess maybe my VPS can't support the function. I can only choice modify php.ini and reboot my web service :-(. Thank you.`
Nano HE
+1  A: 

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 your php.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.

Sarfraz
+1  A: 

You can also use memory_get_peak_usage() to find out which part of the program is eating your memory.

Vili
+2  A: 

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.

Adrian
A: 

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:

preg_match('+<title>(.*)</title>+', $xml_content, $matches);
var_dump($matches);
powtac