我正在使用AdWords报告api来获取所有adwords帐户的所有广告和关键字信息。目前帐户数为1363.报告api生成一个CSV,我将它解析为数组,下面是该函数。解析一个好的1180帐户后,它给出了一个错误解析导致php内存不足的CSV
Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 262144 bytes) .
我已经将内存设置为512MB(我相信是很多)。
的错误出现在这条线
$line_of_text[] = fgetcsv($file_handle, 1024);
私有函数convertCSVtoArray($ csvFile){
try {
$file_handle = fopen($csvFile, 'r');
while (!feof($file_handle)) {
$line_of_text[] = fgetcsv($file_handle, 1024);
}
fclose($file_handle);
return $line_of_text;
} catch (Exception $e) {
log_message('Exception in convertCSVtoArray()', $e);
throw new Exception('Exception in convertCSVtoArray => '.$e);
}
}
上述功能是从方法
$data = $this->convertCSVtoArray($filePath);
我相信所谓,在给定的代码中某个地方存在内存泄漏,它正在占用所有的内存。
请建议是否有解决方案。
那的确是相当有趣的。这是你正在阅读的唯一文件吗?基于记录数(1360)和指定的fgetcsv长度(1024):在我的测试中仅消耗约2-4MB。在运行'convertCSVtoArray'之前,您是否使用了'memory_get_usage'来确保在调用之前内存使用率不是很高? (作为一个可能无关的方面:你应该添加一个检查来确保你在进入该循环之前已经获得了一个读取句柄,如果'fopen'返回false,你会得到一个无限循环,直到你达到内存限制因为'feof'永远不会返回true) – EPB