Fatal Error: Allowed Memory Size of 134217728 Bytes Exhausted (CodeIgniter + XML-RPC)

前端 未结 29 2498
天涯浪人
天涯浪人 2020-11-21 22:57

I have a bunch of client point of sale (POS) systems that periodically send new sales data to one centralized database, which stores the data into one big database for repor

29条回答
  •  南方客
    南方客 (楼主)
    2020-11-21 23:48

    The most common cause of this error message for me is omitting the "++" operator from a PHP "for" statement. This causes the loop to continue forever, no matter how much memory you allow to be used. It is a simple syntax error, yet is difficult for the compiler or runtime system to detect. It is easy for us to correct if we think to look for it!

    But suppose you want a general procedure for stopping such a loop early and reporting the error? You can simply instrument each of your loops (or at least the innermost loops) as discussed below.

    In some cases such as recursion inside exceptions, set_time_limit fails, and the browser keeps trying to load the PHP output, either with an infinite loop or with the fatal error message which is the topic of this question.

    By reducing the allowed allocation size near the beginning of your code you might be able to prevent the fatal error, as discussed in the other answers.

    Then you may be left with a program that terminates, but is still difficult to debug.

    Whether or not your program terminates, instrument your code by inserting BreakLoop() calls inside your program to gain control and find out what loop or recursion in your program is causing the problem.

    The definition of BreakLoop is as follows:

    function BreakLoop($MaxRepetitions=500,$LoopSite="unspecified")
        {
        static $Sites=[];
        if (!@$Sites[$LoopSite] || !$MaxRepetitions)
            $Sites[$LoopSite]=['n'=>0, 'if'=>0];
        if (!$MaxRepetitions)
            return;
        if (++$Sites[$LoopSite]['n'] >= $MaxRepetitions)
            {
            $S=debug_backtrace(); // array_reverse
            $info=$S[0];
            $File=$info['file'];
            $Line=$info['line'];
            exit("*** Loop for site $LoopSite was interrupted after $MaxRepetitions repetitions. In file $File at line $Line.");
            }
        } // BreakLoop
    

    The $LoopSite argument can be the name of a function in your code. It isn't really necessary, since the error message you will get will point you to the line containing the BreakLoop() call.

提交回复
热议问题