douwang9650 2017-09-26 14:29
浏览 246
已采纳

减少PHP中递归函数的内存使用量

I have a recursive function in PHP where it loops in an API where it just allows you to recover 200 records of it at a time.

But since this API has a very high response latency we decided to use a local intermediate database where I add these records and the same will be shown on the site.

However, as this API has more than 30000 records the recursive function it consumes a lot of memory due to it in the case of 30000 records it would have to be recursively invoked more than 1500 times and it ends up giving the famous StackOverflow.

I wonder if there is a manual way to clear the memory of this function by calling it again without losing the value of it.

code example:

public function recursive ($index = 0, $offset = 200) {
   $api = GetConectApi ($index, offset)-> Getrecords ();
   foreach ($api $value) {
      \\Here is my necessary loop
   }
   if (count ($API) > 0) {
      $this Recursive ($index + 200, $offset + 200);
   }
}

I would like to find a way that when it called the recursive function again eliminated the previous allocation without losing the reference value that was passed.

  • 写回答

3条回答 默认 最新

  • dongxing4196 2017-09-26 15:35
    关注

    To expand on user3720435's answer, you are using a lot of memory by creating a new $api variable each time you run the function. To understand why, let's "unroll" the code - imagine it was all written out in sequence with no function calls:

    $api1 = GetConectApi ($index1, offset1)-> Getrecords ();
    foreach ($api1 => $value1) {
        // Here is my necessary loop
    }
    if (count ($api1) > 0) {
        // RECURSION HAPPENS HERE
        $index2 = $index1 + 200, $offset2 = $offset1 + 200
        $api2 = GetConectApi ($index, offset)-> Getrecords ();
        foreach ($api2 => $value2) {
            // Here is my necessary loop
        }
        if (count ($api2) > 0) {
            // RECURSE AGAIN, AND AGAIN, AND AGAIN
        }
    }
    

    Note that I've renamed all the variables as $api1, $api2, etc. That's because each time you run the function, $api is actually a different variable. It has the same name in your source code, but it doesn't represent the same piece of memory.

    Now, PHP doesn't know you're not going to use $api1 again when you create $api2, so it has to keep both in memory; as you end up with more and more sets of data, it needs more and more memory.

    user3720435's suggestion is to add unset($api) before the recursion:

    $api = GetConectApi ($index, offset)-> Getrecords ();
    foreach ($api => $value) {
          // Here is my necessary loop
    }
    if (count ($api) > 0) {
          unset($api);
          // code as before
    }
    

    This tells PHP that you don't need that memory any more, so as it recurses, it won't build up. You'll still build up multiple copies of $index and $offset, but these are likely to be very small in comparison.

    All that being said, it's not clear why you need recursion here at all. The whole thing could actually be changed to a simple loop:

    do {
        $api = GetConectApi ($index, offset)-> Getrecords ();
        foreach ($api => $value1) {
           // Here is my necessary loop
        }
        $index = $index + $offset;
    } while (count ($api) > 0)
    

    A do..while loop always executes once, and then keeps repeating until the condition becomes false. Unrolled it looks like this:

    // do...
        $api = GetConectApi ($index, offset)-> Getrecords ();
        foreach ($api => $value1) {
           // Here is my necessary loop
        }
        $index = $index + $offset;
    if (count ($api) > 0) { // while...
    $api = GetConectApi ($index, offset)-> Getrecords ();
        foreach ($api => $value1) {
           // Here is my necessary loop
        }
        $index = $index + $offset;
    }
    if (count ($api) > 0) { // while...
    // etc
    

    Note that we don't need to allocate any extra memory as we go round the loop, because we haven't entered a new function - we're just using the same variable over and over again.

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?