简体   繁体   中英

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;}
    }
}

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.

您还可以使用memory_get_peak_usage()找出程序的哪一部分正在消耗您的内存。

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.

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

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.

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);

对于共享托管,请把这一行交给您index.php

ini_set("memory_limit","128M");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM