doq1969 2015-12-29 14:15
浏览 21
已采纳

PHP生成器 - 垃圾收集

Simple question.

When or how, by PHP or yourself do generators destroy their stacks?

Take the following example:

function doWork(): Generator
{
    // create some objects.
    $o1 = new stdClass();
    $o2 = new stdClass();

    // pause here and wait for data.
    $value = yield 1;

    // By referencing the above objects, they shouldn't destruct.
    $o1->property = $value;
    $o2->property = $value;

    yield $o1;
    yield $o2;

    // End of stack.
}

// Create the generator.
$generator = doWork();

$value = $generator->current(); // $value will equal 1.

if ($x) {
    $generator->send('Hello, World!'); // Continue execution of the generator.
    $o1 = $generator->current();
    $generator->next();
    $o2 = $generator->current();
    $generator->next(); // Complete the generator

    var_dump($o1);
    var_dump($o2);
} else {
    // Do nothing with the generator.
}

// Carry on with script ...

In this example a generator is started and two objects are created. At this point it is yielded, and further data is requested.

An IF statement is reached.

Case 1

If $x is true, the value "Hello, World!" will be sent to the generator and the objects will be populated with their new properties.

The next time the yielded data is read, the objects will be returned.

Case 2

If $x is false, the generator will no longer be used.

Question

In case 1, I would expect the stack to close like any other function, but what happens to the generator in case 2? Does it and all of the remaining object references stay in memory until the script ends?

Or does the loss of reference to $generator cause it, and all references inside to be cleared out?

  • 写回答

1条回答 默认 最新

  • duanqian3953 2015-12-29 16:23
    关注

    There are two conditions under which a generator destroys its execution context (which also includes the variable table):

    1. If the generator finishes execution. This may either happen by executing a return (including the implicit return at the end of the function) or through an uncaught exception during execution of the generator.
    2. If all references to the generator object are relinquished.

    So, no, the generator will not live until the script ends. It will be destroyed once the $generator variable goes out of scope, at which point the generator will relinquish its references to the variable values and other execution state.

    You can easily observe destruction order by creating a class that echos in the destructor and then instantiating this class into a local variable.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建