I have memory_limit=32M in php.ini and yet I get the exhausted error messages.

I place echo memory_get_usage() in various parts of the script just to see how much memory is being used and yet the results are no way near 32M!

Near start of script echo memory_get_usage() displays 211,192
After reading mysql data: 226,000
After displaying data in tables: 243,024

So how come the 32M is being exhausted if only a max of 243K is being used by the script?


I did think it was to do with the way I read from the database. The table is about 80Mb in size and the script reads in various passes. In order save on disk access time I store the table in a heap and run the script data reading from the heap.

$query = "create temporary table $temp_table type=heap select from $table where $name = '$name'";
.
.
$query = "select
from $temp_table";
while ($row = @mysql_fetch_row($result)) {
.
.
}

So I thought this was the problem but that works just fine. Besides, it would be MySql which would barf at this point due to memory errors.

Any ideas why php is failing with exhausted memory?

    does your server have 32megs available to you? Just because php is set to 32mb, doesn't mean it can always get that much

    adam

      Sure, there is 1Gig in there:

      Memory : 1015300 kB
      Total Usage %
      Used 468708 46 %
      Free 546592 kB 54 %
      Buffered 45208 kB 4 %
      Cached 307004 kB 30 %

      Swap : 1052216 kB
      Total Usage %
      Used 0 0 %
      Free 1052216 kB 100 %

      Mounts
      Mount Size Free Used Usage %
      / 37G 32G 4.7G 13%
      /dev/shm 256M 256M 0 0%

      If I up the memory limit to 64M I don't get the problem. So that indicates to me that the php script is hitting the 32M limit but not using much more than that. I just can't understand why it uses so much memory when memory_get_usage() says just 243k is being used 😕

        are you echoing the memory usage in the while loop...sounds like you are hitting the memory limit within the reads...maybe echo the memory get usage in the while loop...see if it hitting it's head within there.

          That's a good point.

          What I have done now is to place this:

          $max_mem = max(memory_get_usage(), $max_mem);

          in various parts of the script and at the end echo $max_mem

          I get 454,680

          So even in the middle of the loops, after opening the tables, creating the hashes, generating the tables the max memory used is less than 0.5 Mb

            I have checked the logs again and it seems the error could be because of an include file:

            include 'parse_variables.php';

            This is actually within the database loop so is this wrong? Should I use include once or require instead?

            while ($row = @mysql_fetch_row($result)) {

                 .
                 .
                  //other stuff here
            
                 if($row[2] > 9 ) {
            
                   include 'parse_variables.php';
            
                   //display tables here
            
                 }
            
                 .
                 .

            }

              The problem is definately to do with the include.

              I cut and pasted the include file into the script and I don't get memory exhausted messages.

              The loop is iterated about 10,000 times so clearly php has an issue with including a file that many times!

              Is there another way around this? I would like to utilise an include file method as the parse_variables.php is a standard file I use across many scripts. It is basically a load of switch statements used to replace database mnemonics with full text.

              If I paste it into each file it is messy and means I have to update a dozen files if there are any changes.

              I don't think include_once is suitable as doesn't that mean that the parse_variable functions are only evaluated once and not on every iteration of the loop?

                I think I have worked this out now.

                The parse_variables.php does not contain functions but just a block of code I cut and paste from the origing file.

                What I need to do is to include the file at the begining of the calling script and then use a function to call the parse_variable code.

                main.php

                <?php
                include 'parse_variables.php';
                .
                .
                .
                while (......

                proc_parse_variables();
                .
                .
                .
                }

                parse_variables.php

                <?php
                function proc_parse_variables()
                //parse_variable code here
                }

                Silly me. The server must have eaten up a zillion bytes during the past few days with all those includes within the loop
                🙂

                  I learned from this myself...as far as the include..zillion bytes....😃

                    Write a Reply...