Jump to content

Fatal error: Allowed memory size


sniperscope

Recommended Posts

hello

I am getting Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 32 bytes) in /home/accnt/public_html/connection/class_query.php on line 499

 

Code is below: i have no idea why is this happening...

function WorkPanel()
{
 $count    = 0;
 $today    = date("D", $this->Day1());
 $day_change= $this->StoreInfo("day_change");
 $arr = array();
 $hr  = date("H");
 $min = (date("i") < 30) ? "00" : "30";
 $now = strtotime($hr .":". $min);
 $sql   = mysql_query("SELECT " .$today. " FROM work WHERE " .$today. " != '' AND area = '" .$this->area. "'");
 while($row = mysql_fetch_array($sql))
 {
  $working_hour = explode("~", $row[$today]);

  $start_hour = strtotime($working_hour[0]);
  $end_hour = strtotime($working_hour[1]);

  if($working_hour[1] >= "00:00" && $working_hour[1] <= "07:00")
  {
   $mid_nght = strtotime("23:59");

   for($i = $start_hour; $i <= $mid_nght; $i += 1800)
 $arr[] = $i;

   array_push($arr, $i);

   $over_mid_start = strtotime("00:30");
   $over_mid_end = strtotime($bitis);

   for($i = $over_mid_start; $i <= $over_mid_end; $i += 1800)
 $arr[] = $i;
  }
  else
  {
   if($working_hour[1] == "00:00")
 $mid_nght = "23:59";

   for($i = $start_hour; $i <= $mid_nght; $i += 1800)
 $arr[] = $i;

   if($working_hour[1] == "00:00")
 array_push($arr, $i);
  }
  if(in_array($now, $arr))
   $count++;
  unset($arr);
 }
 return $count;
}

 

Regards

Link to comment
https://forums.phpfreaks.com/topic/273947-fatal-error-allowed-memory-size/
Share on other sites

The problem may not even be related to that function, it is just that on line 499 PHP ran out of memory. It could have happened anywhere between line 1 and 499. The only way to solve this, is to benchmark every function (method) until you narrow it down. Then benchmark every line in that function until you get to the one using the most offensive amount of memory.

 

Example: UN-TESTED



[code]
function WorkPanel()
{
$start_bench = memory_get_usage(); //benchmark
$count = 0;
$today = date("D", $this->Day1());
$day_change= $this->StoreInfo("day_change");
$arr = array();
$hr = date("H");
$min = (date("i") < 30) ? "00" : "30";
$now = strtotime($hr .":". $min);
$sql = mysql_query("SELECT " .$today. " FROM work WHERE " .$today. " != '' AND area = '" .$this->area. "'");
while($row = mysql_fetch_array($sql))
{
$working_hour = explode("~", $row[$today]);

$start_hour = strtotime($working_hour[0]);
$end_hour = strtotime($working_hour[1]);

if($working_hour[1] >= "00:00" && $working_hour[1] <= "07:00")
{
$mid_nght = strtotime("23:59");

for($i = $start_hour; $i <= $mid_nght; $i += 1800)
 $arr[] = $i;

array_push($arr, $i);

$over_mid_start = strtotime("00:30");
$over_mid_end = strtotime($bitis);

for($i = $over_mid_start; $i <= $over_mid_end; $i += 1800)
 $arr[] = $i;
}
else
{
if($working_hour[1] == "00:00")
 $mid_nght = "23:59";

for($i = $start_hour; $i <= $mid_nght; $i += 1800)
 $arr[] = $i;

if($working_hour[1] == "00:00")
 array_push($arr, $i);
}
if(in_array($now, $arr))
$count++;
unset($arr);
}
echo memory_get_usage() - $start_bench; //print amount of memory this function uses.
return $count;
}

Dear Jcbones

I am so thank you for that function. I even doidn:t know that php has so useful function.

 

Okay, i guess i found what makes that problem.

Seems, first loop cause it, at

for($i = $start_hour; $i <= $mid_nght; $i += 1800)

$arr[] = $i;

 

So i remove it(commented) and than script throw me error on second loop.

And i removed it as well, whoola page load fine.

 

I do not want to touch to server's max_memory because this is at test now and if only visitor(me) make that much problem, i cannot imagine 1000 visitors at same time.

 

So, i think, i have to re-think and optimize and re-create my codes from all over again.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.