dongpo5239 2016-07-30 08:11
浏览 47

在PHP中解析CSV导致内存不足

I am using adwords reporting api to get all ad's and keyword's info for all the adwords account. Currently account count is 1363. Reporting api generates a CSV which I am parsing it to Array below is the function. After parsing a good 1180 account it gives an error

Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 262144 bytes) .

I have set memory to 512MB (which I believe is a lot).

The error comes on this line

$line_of_text[] = fgetcsv($file_handle, 1024);

private function 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);
    }
}

The above function is called from a method

$data = $this->convertCSVtoArray($filePath);

I believe, there is a memory leak somewhere in the given code which is eating all the memory.

Please suggest if there is any solution.

  • 写回答

1条回答 默认 最新

  • dongxie9448 2016-07-30 08:28
    关注

    To change the memory limit for one specific script, include a line such as this at the top of the script:

    ini_set("memory_limit","12M");

    You can also make this a permanent change for all PHP scripts running on the server by adding a line such as this to the server’s php.ini file:

    memory_limit = 12M

    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)