一般会使codeigniter对象或php无效吗?

I am using codeigniter and PHP to build a web app and there are lot of objects that I have to define/use inside models due to large number of queries that I have to execute. Sometime I have to use same object multiple times inside a loop, for example:

  $dbData = $this->mydb->query($dbQuery);
  if($dbData->num_rows() >0 ){ //check if rows were returned
     $dbResult = $dbData->result();
     foreach( $dbResult as $index => $row){
        $dbArray[$row->JOB_NAME_STG] = $row->REC_LOADED;
    }

In general programming practice you should nullify the objects once they are no longer required but I haven't been able to find any detail on the best way to ensure that all the objects are nullified avoiding memory leaks for codeigniter or php in general.

Is assigning NULL to an object at the end of the function sufficient?

$dbData = $this->mydb->query($dbQuery);
$dbData = NULL;

Or codeigniter has more efficient way of ensuring that there are no memory leaks.

Should object be nullified inside a loop before using to ensure there is no memory leak?

Any pointers in the direction will be appreciated. Thanks!!!

duan0427
duan0427 我认为你不应该取消指向对象的指针......你应该取消它们。如果你使指针无效,那么指针现在指向值NULL并失去对原始内存的引用...因此强制垃圾收集将其排序!至少,你可以这样做:unset($dbData);$dbData=NULL;以该顺序。
5 年多之前 回复
doutong7216
doutong7216 我永远不会使任何物体无效。我认为我们不应该这样做
大约 8 年之前 回复
dqqyp90576
dqqyp90576 “在一般的编程实践中,一旦不再需要它们就应该使对象无效”---谁告诉你的?这不是真的。
大约 8 年之前 回复

2个回答

Assigning null makes no sense in:

  1. php >= 5.3
  2. applications, that don't work for hours

Do you match both items above?

dongxi0523
dongxi0523 在php 5.3中引入了新的智能垃圾收集器
大约 8 年之前 回复
drzip28288
drzip28288 你能告诉我一些关于为什么在php> = 5.3中不需要NULL的细节吗?
大约 8 年之前 回复
dongyu9850
dongyu9850 所以 - 没有意义
大约 8 年之前 回复
donglan9651
donglan9651 我在PHP版本5.3.10和我的脚本运行10-20分钟..但不是几个小时
大约 8 年之前 回复



unset()应该从内存中释放变量或对象。 但是,PHP没有很好的垃圾收集。 他们试图用5.3的新循环垃圾收集来改进它,但它仍然存在挑战。</ p>
</ div>

展开原文

原文

unset() is supposed to free the variable or object from memory. However, PHP doesn't have very good garbage collection. They are trying to improve it with their new circular garbage collection with 5.3 but it still has challenges.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐