PHP< 5.3垃圾收集,做数组值需要被设置为空或不设置阵列= NULL孤儿的所有元素? [英] php < 5.3 garbage collection, do array values need to be set null or does setting the array = null orphan all its elements?

查看:170
本文介绍了PHP< 5.3垃圾收集,做数组值需要被设置为空或不设置阵列= NULL孤儿的所有元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我使用PHP 5.2和需要一些垃圾收集,因为我处理的非常有限的资源和大型数据集。

so I am using php 5.2 and needing some garbage collection since I am dealing with very limited resources and large data sets.

这是我的测试中,我已经看到,未设置什么也不做,直到脚本(即使我耗尽内存),这似乎有点违背了文档的末尾,虽然我认为我也读了5.3文档不是5.2的文档和5.3文档似乎相对无证。

from my tests I have seen that unset does nothing until the end of the script(even if I run out of memory), which seems a little bit contrary to the documentation, although I assume that I am also reading the 5.3 docs not the 5.2 docs and the 5.3 docs seem relatively undocumented.

我班的一个准系统示例如下:

An barebones sample of my class is as follows:

class foo{
private $_var;

public function __construct(){
  $this->_var = array();
  for($i = 0; $i < 10000000000; $i++){
       $this->_var[rand(1, 100000)] = 'I am string '.$i.' in the array';
  }
}

  public function myGC(){
    $this->_var = null;
  }
}

在我的功能myGC()'我应该做一个foreach阵列上,并设置我遇到为NULL每个元素(我记得在做C ++)或将设置$本 - > _ VAR = NULL自由不仅指针该阵列还与指针相关的所有元素?

in my function 'myGC()' should I do a foreach over the array and set each element I encounter to NULL (as I remember doing in C++) or would setting $this->_var = NULL free not only the pointer to the array but also all elements associated with the pointer?

推荐答案

这足以置 $这个 - &GT; _var = NULL ,这释放内存的一切 $这个 - &GT; _var 设置为。

It's enough to set $this->_var = NULL, this frees the memory for everything $this->_var was set to.

您可以用这个(伪code)测试

You can test it with this (pseudo code)

echo 'before: '.memory_get_usage().'</br>';
$Test = foo();
echo 'after class instance: '.memory_get_usage().'</br>';
$Test = foo->myGC();
echo 'after unset of _var: '.memory_get_usage().'</br>';
$Test = NULL;
echo 'after unset of object: '.memory_get_usage().'</br>';

这篇关于PHP&LT; 5.3垃圾收集,做数组值需要被设置为空或不设置阵列= NULL孤儿的所有元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆