dowe98261 2011-11-26 12:11
浏览 52
已采纳

深度递归内存泄漏 - 取消了一个fopen资源?

I have a recursive function to iterate over 11M database records, 1000 at a time. As it approached 9M it stopped. My assumption of a memory problem was confirmed when I displayed get_memory_usage() after every 1000 records.

The function works something like this:

<?
get_data_block();

function get_data_block($id=0);
{
    //open a csv file for writing
    $packages_sorted_csv=fopen("./csv/packages_sorted.csv", "a");

    //get 1000 records and process them
    //$unsorted = array of 1000 records from database
    foreach($unsorted as $row);
    {
        $ct++;
        $id++;
        //$packages_sorted = array of processed data

        //write output
        fputcsv($packages_sorted_csv, $packages_sorted);
    }
    fclose($packages_sorted_csv);

    if($ct==1000)
    {
        unset($unsorted);
        echo 'Mem usage: '.memory_get_usage();
        get_data_block($id);    //call the function again
    }else{
        //finished
    }
}

?>

Does anyone have a tip about how to release all resources with recursive functions? ...or is there a way to call the same function again so it's not called by itself?

Notes:

  • I have to chunk the data in blocks to free up the busy mysql server.
  • I have tried unsetting every defined variable that's not in the global scope.
  • The only thing I can't seem to unset is the fopen resource.
  • The memory size grows by about 400k each iteration.
  • 写回答

2条回答 默认 最新

  • dongzhan3937 2011-11-26 12:20
    关注

    From the code you posted, recursion doesn't seem like the right approach.

    Build a second function and rearrange that one so that you can do something like (pseudo-code):

    while (!$done) {
      $id = get_data_block($id);
      $done = // determine if finished or not
    }
    

    i.e. return $id from get_data_block, giving it an invalid value if no more processing needs to be done. You won't have to worry about the stack frames piling up this way.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)