php

时间:2021-06-09 21:16:34

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

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

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.

从我的测试中我看到unset在脚本结束之前什么都不做(即使我内存不足),这似乎与文档有点相反,尽管我假设我也在阅读5.3文档而不是5.2 docs和5.3文档看起来相对没有文档。

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;
  }
}

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?

在我的函数'myGC()'中我应该对数组做一个foreach并将我遇到的每个元素设置为NULL(我记得在C ++中做)或者设置$ this - > _ var = NULL不仅仅是指向数组的指针还有与指针相关的所有元素?

2 个解决方案

#1


4  

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

设置$ this - > _ var = NULL就足够了,这就释放了$ this - > _ var设置为的所有内存。

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>';

#2


0  

You don't call myGC() anywhere, is this the problem?

你不在任何地方调用myGC(),这是问题吗?

To test the assumption about unset not working, try running the below example. If it fails, your assumption is correct. If not - you have some other error.

要测试关于unset not working的假设,请尝试运行以下示例。如果失败,您的假设是正确的。如果没有 - 你还有其他一些错误。

class foo{
    private $_var;

    public function __construct(){
      $this->_var = array();
      for($i = 0; $i < 10000000000; $i++){
           $this->_var[$i] = 'I am string '.$i.' in the array';
           unset($this->_var[$i]);
      }
    }
}
$f=new foo();

#1


4  

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

设置$ this - > _ var = NULL就足够了,这就释放了$ this - > _ var设置为的所有内存。

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>';

#2


0  

You don't call myGC() anywhere, is this the problem?

你不在任何地方调用myGC(),这是问题吗?

To test the assumption about unset not working, try running the below example. If it fails, your assumption is correct. If not - you have some other error.

要测试关于unset not working的假设,请尝试运行以下示例。如果失败,您的假设是正确的。如果没有 - 你还有其他一些错误。

class foo{
    private $_var;

    public function __construct(){
      $this->_var = array();
      for($i = 0; $i < 10000000000; $i++){
           $this->_var[$i] = 'I am string '.$i.' in the array';
           unset($this->_var[$i]);
      }
    }
}
$f=new foo();