我的php内存不足,出现服务器错误"内存不足:终止进程"。大约 25% 的流程" 尽管它搜索了大约 10,000 行,但与条件匹配的行数(因此需要在流程结束时存储并写入文件(少于 200 行。 所以我不确定为什么它会耗尽内存。
我收到此错误是因为每次循环后没有清除变量,还是需要增加服务器上的内存?
流程简单如下: - LOOPA - 循环浏览400个邮政编码的列表 - 对每个 zip 使用一个 API 调用 - 获取每个 zip 中所有位置的列表(通常约为 40-50(
-- SUBLOOP1 - 对于找到的每个地点,使用 API 调用获取该地点的所有事件
---- SUBLOOP1A循环事件以计算每个地点的数量
zips = file($configFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$dnis = file($dniFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$s3->registerStreamWrapper();
$file = fopen("s3://{$bucket}/{$key}", 'w') or die("Unable to open file!");
fwrite($file, $type . " id" . "t" . $type . " name" . "t" . "zip" . "t" . "event count" . "n" );
foreach($zips as $n => $zip){
//first line is the lable to describe zips, so skip it
if ($n < 1) continue;
$params = $url;
$params .= "&q=" . $zip;
$more_node_pages = true;
while ($more_node_pages){
$res = fetchEvents($params);
//Now find the number of events for each place
foreach($res->data as $node){
//first check if on Do Not Include list
$countevents = true;
foreach($dnis as $dni) {
if ($dni == $node->id) {
echo "Not going to get events for ". $node->name . " id# " . $dni . "nn";
$countevents = false;
break;
}
}
//if it found a match, skip this and go to the next
if (!$countevents) continue;
$params = $url . $node->id . "/events/?fields=start_time.order(reverse_chronological)&limit=" . $limit . "&access_token=". $access_token;
//Count the number of valid upcoming events for that node
$event_count = 0;
$more_pages = true;
$more_events = true;
while ($more_pages) {
$evResponse = fetchEvents($params);
if (!empty($evResponse->error)) {
checkError($evResponse->error->message, $evResponse->error->code, $file);
}
//if it finds any events for that place, go throught each event for that place one by one to count until you reach today
foreach($evResponse->data as $event){
if(strtotime($event->start_time) > strtotime('now')){
$event_count++;
}
//else we have reached today's events for this node, so get out of this loop, and don't retrieve any more events for this node
else {
$more_events = false;
break;
}
}
if (!empty($evResponse->paging->next) and $more_events) $params = $evResponse->paging->next;
else $more_pages = false;
} //end while loop looking for more pages with more events for that node (page)
if ($event_count > "0") {
fwrite($file, $node->id . "t" . $node->name . "t" . $zip . "t" . $event_count . "n");
echo $event_count . "n";
}
} // loop back to the next place until done
//test to see if there is an additional page
if (!empty($res->paging->next)) $params = $res->paging->next; else $more_node_pages = false;
} //close while loop for $more_node_pages containing additional nodes for that zip
} // loop back to the next zip until done
fclose($file);
我强烈建议在每个嵌套循环的开头添加输出。我认为您很可能有一个无限循环,这会导致脚本内存不足。
如果不是这种情况,那么您可以尝试通过将以下 PHP 行添加到脚本顶部来增加 PHP 脚本的内存限制:
ini_set("memory_limit", "5G");
如果您的脚本需要超过 5GB 的 RAM 来处理 400 个邮政编码,我建议您分解您的脚本,以便您可以运行邮政编码 0-10,然后是 11-20,然后是 21-30,依此类推。
希望这有帮助,干杯。
您需要找出内存丢失的位置,然后您可以处理它或解决它。 memory_get_usage()
是你的朋友 - 用一些标识符在每个循环的顶部(或底部(打印它,这样你就可以看到你何时何地用完了内存。