PHP Fatal error: Allowed memory size of 134217728 bytes exhausted

Any clues on how to get rid of this PHP error?

[26-Jan-2010 00:53:22] PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 71 bytes) in /website/www/includes/functions_manpages.php on line 58
[26-Jan-2010 01:38:37] PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 71 bytes) in /website/www/includes/functions_manpages.php on line 58
[26-Jan-2010 04:33:40] PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 71 bytes) in /website/www/includes/functions_manpages.php on line 58
[26-Jan-2010 05:33:16] PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 71 bytes) in /website/www/includes/functions_manpages.php on line 58
[26-Jan-2010 07:36:43] PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 71 bytes) in /website/www/includes/functions_manpages.php on line 58
[26-Jan-2010 10:32:52] PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 71 bytes) in /website/www/includes/functions_manpages.php on line 58

Offending code fragment below:

foreach ( $matchWords as $key=>$item ) {
          if ( $item == '' || in_array(strtolower($item), $stopWords) || strlen($item) <= 3 ) {
              unset($matchWords[$key]);  //offending line 58
            }
          if ( preg_match('/^[ \t]+/',$item) ) {
              unset($matchWords[$key]);
            }
          }

---------- Post updated at 20:41 ---------- Previous update was at 15:26 ----------

I think I fixed this......

The problem seemed to be short queries (one letter, for example) in apropos (all sections) generating too large of search results.

Basically, I have limited the queries, something like this:

if (!preg_match("/^\w[\w\-]+$/",$query) && !preg_match("/^\w\w+\.\w+$/",$query) && !preg_match("/^\w\w+::\w+$/",$query)) {
        notfound();
        $showresults=0;
    }

Surely there is a cleaner, better, nicer way... but I'll leave that for another day :smiley:

The heap size is in the php.ini if you want to increase it.

It's pretty common to need to bump it up a bit higher for some applications.

Thanks.

I think we are OK as long as we disallow single letter apropos queries, etc.

In addition, I have made a few other changes so people cannot search with hacker strings like ../.. etc. etc.

.... a kind of "Explicit Allow" for query strings to the man command to try to minimize hacker play on the man page search engine.

Still need more work.

OBTW, we are already starting to see a number of Google search referrals to our new man pages, and we rank pretty high for such a short time in the playing field.

I have not been able to trap every possible odd query that can cause this error without causing a redirect problem (and am a bit lazy to keep trying ...) so I am trying this in the offending PHP function:

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

Was

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

OK, finally, 8 months after posting this php memory bug, I figured it out, hahaha.

Basically, I used the PHP error_log() function to print the memory allocation using memory_get_usage() and discovered that an array was using a lot of memory, and traced it to $string (using strlen($string) in the debug log) that was passed to the function used in a regex to create the array.

As a temporary fix, I created a PHP conditional to test the memory usage and avoid the offending foreach statement until I could come up with a solution (debugging the entire process, step-by-step).

The solution was to simply define MAX_STR and take a sub string of the huge $string with substr($string,0,MAX_STR), which was using all the memory in the foreach loop in the function.

Then, to clean up, I defined MAX_MEM to insure that the maximum memory of this PHP thread would alway be less than the max PHP memory allocated in the php.ini file.

define('MAX_STR',512000);
define('MAX_MEM',96000000);
$string = substr($string,0,MAX_STR);
if(memory_get_usage() < MAX_MEM){//blah blah}

Housekeeping work... done!