sniperscope Posted February 2, 2013 Share Posted February 2, 2013 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 Quote Link to comment Share on other sites More sharing options...
trq Posted February 2, 2013 Share Posted February 2, 2013 Are you sure that is the relevant code? It doesn't look like it belongs in the file described in the error message. As for your error. Your running out of memory. Usually from doing some pretty intensive task. Quote Link to comment Share on other sites More sharing options...
sniperscope Posted February 2, 2013 Author Share Posted February 2, 2013 Yes that function belong to query class and i guess problem is array That function display some data at left side of side and it is include every single page. Quote Link to comment Share on other sites More sharing options...
jcbones Posted February 2, 2013 Share Posted February 2, 2013 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; } Quote Link to comment Share on other sites More sharing options...
sniperscope Posted February 2, 2013 Author Share Posted February 2, 2013 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.