在哪里可以找到"低内存"和"免费CPU周期"电话上未设置触发垃圾收集()? [英] Where to find the "low memory" and "free CPU cycles" calls triggering garbage collection on unset()?

查看:156
本文介绍了在哪里可以找到"低内存"和"免费CPU周期"电话上未设置触发垃圾收集()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常发现以下引用并解释说,一个PHP 未设置()不会触发垃圾收集立即,但是当它看到只有合适时使用引用(重点煤矿):

I often find references to the following quote being used when explaining that a PHP unset() doesn't trigger "garbage collection" immediately, but only when it sees fit (emphasis mine):

未设置()不正是它的名字说:
   - 取消设置一个变量。它不强制立即内存释放。 PHP的
  当垃圾收集器会做
  看到千篇一律 - 意向尽快,因为
  不需要那些 CPU周期,无论如何,
  或迟至前脚本将
  耗尽内存,无论发生什么,
  第一位。

unset() does just what it's name says - unset a variable. It does not force immediate memory freeing. PHP's garbage collector will do it when it see fits - by intention as soon, as those CPU cycles aren't needed anyway, or as late as before the script would run out of memory, whatever occurs first.

如果你正在做什么$ = NULL;
  那么你就重写变量
  数据。你可能会释​​放内存/
  缩水更快,但它可能会盗取CPU
  从code,真正需要的周期
  它们更快,从而导致较长的
  总执行时间。

If you are doing $whatever = null; then you are rewriting variable's data. You might get memory freed / shrunk faster, but it may steal CPU cycles from the code that truly needs them sooner, resulting in a longer overall execution time.

我想知道C code表示这个内存不足和自由CPU周期,引发垃圾收集的怎么工作的,是否PHP 5.2.x和PHP 5.3 +之间是不同的。

I want to know how the C code for this "low memory" and "free CPU cycles" triggering of the garbage collection works exactly and whether it differs between PHP 5.2.x and PHP 5.3+.

所以,我下载PHP 5.2.17的C源文件,并试图找到合适的code部分。也许我只是盲目的,还是我的C技能太低了,但我没能找到这样code。

So I downloaded the C source files of PHP 5.2.17 and tried to locate the proper code sections. Maybe I'm just blind, or my C skills are too low, but I failed to find such code.

有人可以点我到正确的C文件,请?

Can somebody point me to the proper C files, please?

修改

当搜寻上述报价的示例用法,我意识到一些奇怪的。

While searching for example usages of the above quote, I realized something weird.

的一些实施例中,如 http://stackoverflow.com/a/584982/693207 时,参照该通过以下URL上php.net评论报价:的http:/ /us2.php.net/manual/en/function.unset.php#86347

Some of the examples, like http://stackoverflow.com/a/584982/693207, reference to this quote by using the following URL to a comment on php.net: http://us2.php.net/manual/en/function.unset.php#86347.

浏览到该URL只显示了未设置()手动的顶部。评论#86347丢失。

Browsing to this URL only shows the top of the unset() manual. Comment #86347 is missing.

检查自由之路机显示,这评论<一个href=\"http://web.archive.org/web/20120910142543/http://us2.php.net/manual/en/function.unset.php#86347\"相对=nofollow>的确从2008年10月存在,但还是之后的某个消失了2012年9月(原因不详)。

Checking the wayback machine shows, that this comment DID exist from Oct 2008, but vanished sometime in or after Sep 2012 (reason unknown).

也许这报价,始终是,完全错误的?

Maybe that quote is, and always was, just plain wrong?

或者是没有人在那里,谁可以点我到正确的C文件?

Or is there anybody out there, who can point me to the proper C files?

推荐答案

确定,所以现在是时候让一些PHP流言终结者!通过阅读垃圾收集是如何工作的PHP文件请开始,因为我将承担这一切是如何工作的一些先验知识:

OK, so it's time for some PHP Mythbusters! Please start by reading the PHP documentation on how garbage collection works, as I'm going to assume some prior knowledge of how this all works:

  • Reference Counting Basics
  • Collecting Cycles

第二份文件专门解释了什么是引发周期性的垃圾收集器的运行。它无关,与自由CPU周期或内存不足 - 它是完全基于是present潜在的垃圾对象的数目:

The second document specifically explains what triggers the running of the cyclic garbage collector. It has nothing to do with "free CPU cycles" or "low memory" — it's based entirely on the number of potential garbage objects that are present:

在垃圾收集器被接通时,每当根缓冲器运行满时执行上述的循环寻找算法。根缓冲区有10000可能根固定大小。

When the garbage collector is turned on, the cycle-finding algorithm as described above is executed whenever the root buffer runs full. The root buffer has a fixed size of 10,000 possible roots.

即,环状垃圾收集运行潜在地垃圾对象的一定数目已经累积的任何时间,而不论这些对象的大小。审查 的code zend_gc.c 证实了这一点 - 当然没有什么在那里检查可用内存的总量,肯定有没有这将需要做,而CPU的运行GC线程的免费。因此,我认为我们可以称之为这部分捣毁。

That is, the cyclic garbage collector runs any time that a certain number of potentially garbage objects have accumulated, irrespective of the size of those objects. Reviewing the code in zend_gc.c confirms this — there is certainly nothing in there checking the overall amount of free memory, and there's certainly none of the threading that would be needed to make the GC run while the CPU is free. So I think we can call this part "busted".

接下来,让我们来看看什么 $ X = NULL 未设置($ X)可能。首先,让我们证实,他们确实使用这个类作为我们的巴斯特假人一样的东西:

Next, let's take a look at what the actual difference between $x = null and unset($x) might be. First, let's confirm that they do the same thing using this class as our Buster the Test Dummy:

class NoisyDestructor {
    function __destruct() {
        print "Destructor called\n";
    }
}

现在,让我们来看看有什么设置变量为null,未设置() -ing它的区别是:

Now, let's see what the difference between setting a variable to null and unset()-ing it is:

$x = new NoisyDestructor();
print "Created\n";
$x = null;
print "Nulled\n";

print "\n";

$x = new NoisyDestructor();
print "Created\n";
unset($x);
print "Unset\n";

当我们运行这一点,我们可以看到:

When we run this, we see:

Created
Destructor called
Nulled

Created
Destructor called
Unset

等一下 - 这是两个完全一样的序列!有两者之间没有功能上的差异。现在,如何表现?

Wait a minute — that's the exact same sequence for both! There's no functional difference between the two. Now, how about the performance?

class Thing { }

$start = microtime(true);
for ($i = 0; $i < 1e6; $i++) {
    $x = new Thing();
    $x = null;
}
printf("%f sec for null\n", microtime(true) - $start);

$start = microtime(true);
for ($i = 0; $i < 1e6; $i++) {
    $x = new Thing();
    unset($x);
}
printf("%f sec for unset\n", microtime(true) - $start);

现在,用我的笔记本电脑进行测试,用PHP 5.4,我得到:

Now, using my laptop to test, with PHP 5.4, I get:

0.130396 sec for null
0.175086 sec for unset

不仅是设置变量为null和取消它的性能差异pretty未成年人,考虑我们有多少次运行这个循环看到这样的结果,但它实际上是在完全相反什么评论声称:未设置()慢约25%!这个PHP神话一直很好,真正捣毁。

Not only is the performance difference between setting a variable to null and unsetting it pretty minor, considering how many times we had to run this loop to see this result, but it's actually the exact opposite of what that comment claimed: unset() is about 25% slower! This PHP myth has been well and truly busted.

TL; DR :您发现该帖是完全错误的。 (这很可能是它从PHP.net删除这个确切原因。)

TL;DR: The quote you found is completely wrong. (It seems likely that it was removed from PHP.net for this exact reason.)

这篇关于在哪里可以找到&QUOT;低内存&QUOT;和&QUOT;免费CPU周期&QUOT;电话上未设置触发垃圾收集()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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