doubianyan9749 2009-02-09 21:55
浏览 106
已采纳

PHP循环引用问题的规模是什么,我应该担心吗?

If I am using a tree structure of nodes similar to the code below, do I have to worry about the circular reference?
I have read that PHP uses a memory allocation mechanism which can make life very hard for the garbage collector when there are circular references involved.

What I want to know is:

  • If my tree consists of only a few nodes, say 25, is this a problem?
  • Will the memory be freed at the end of the script or am I slowly creating a problem for the server?
  • Under what circumstances will this problem have an effect during script execution?
  • Will manually destroying the references solve the problem and should I always do it?
class Node {
    private $parent;
    private $children;

    function addChild( Node $child ) {
        $this->children[] = $child;
        $child->setParent( $this );
    }

    function setParent( $parent ) {
        $this->parent = $parent;
    }
}

//eg
$node0 = new Node;
$node1 = new Node;

// nodes 1 and 2 have a circular reference to each other
$node0->addChild( $node1 );
  • 写回答

4条回答 默认 最新

  • dsndm82062 2009-02-09 22:21
    关注

    Point by point:

    • If my tree consists of only a few nodes, say 25, is this a problem?

    Not unless your nodes are real monsters.

    • Will the memory be freed at the end of the script or am I slowly creating a problem for the server?

    When the interpreter shuts down all the memory is released.

    • Under what circumstances will this problem have an effect during script execution?

    I doubt you will have anything to worry about unless you have very low memory limits or very large dynamic data structures. If you have 25 nodes that aren't being created/freed frequently you won't have an issue.

    • Will manually destroying the references solve the problem and should I always do it?

    It will help. When loading a large data set into our database with Propel we ran into a lot of problem with memory consumption that we tracked to circular references not being freed. Our solution was to call a method that cleared all references.

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

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大