PHP脚本运行时垃圾收集 [英] php garbage collection while script running

查看:98
本文介绍了PHP脚本运行时垃圾收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP脚本,运行在cron上,最多可能需要15分钟才能执行。定期间我会吐出memory_get_usage(),所以我可以看到发生了什么。第一次告诉我我的使用量是10万。当脚本完成时,我是114美元!



PHP脚本运行时是否做垃圾回收?或者所有记忆发生了什么?有什么我可以做的来强制垃圾收集。我的脚本正在做的任务是每隔几个节点导入Drupal。所以它做同样的事情很多次。



任何建议?

解决方案

关键是您不需要取消设置您的全局变量。 / p>

您不需要对局部变量和对象属性显式调用unset,因为当函数超出范围或对象被销毁时,它们被破坏。



一旦引用计数变为零,PHP会保留所有变量的引用计数并将其破坏(大多数情况下)。对象有一个内部引用计数,变量本身(对象引用)每个都有一个引用计数。当所有的对象引用已经被破坏,因为它们的引用coutns已经达到0,对象本身将被销毁。示例:

  $ a = new stdclass; // $ a zval refcount 1,object refcount 1 
$ b = $ a; // $ a / $ b zval refcount 2,object refcount 1
//这将强制zval分离,因为$ b不是引用集的一部分:
$ c =& $ a; // $ a / $ c zval refcount 2(isref),$ b 1,object refcount 2
unset($ c); // $ a zval refcount 1,$ b 1,object refcount 2
unset($ a); // $ b refcount 1,object refcount 1
unset($ b); //一切都被破坏

但请考虑以下情况:

  class A {
public $ b;
}
class B {
public $ a;
}

$ a = new A;
$ b = new B;
$ a-> b = $ b;
$ b-> a = $ a;
unset($ a); //不能销毁对象$ a,因为$ b仍然引用它
unset($ b); //不能破坏对象$ b,因为$ a仍然引用它

这些循环引用是PHP 5.3垃圾收集器启动。您可以使用 gc_collect_cycles 显式调用垃圾回收器/ a>。



另请参见参考计数基础收集手册中的周期


I have a PHP script that runs on cron that can take up to an 15 minutes to execute. At regular intervals I have it spitting out memory_get_usage() so I can see what is happening. The first time it tells me my usage I am at 10 megs. When the script finishes I am at 114 megs!

Does PHP do it's garbage collection while the script is running? Or what is happening to all that memory? Is there something I can do to force garbage collection. The task that my script is doing is a nightly import of a couple thousand nodes into Drupal. So it is doing the same thing a lot of times.

Any suggestions?

解决方案

The key is that you unset your global variables as soon as you don't need them.

You needn't call unset explicitly for local variables and object properties because these are destroyed when the function goes out of scope or the object is destroyed.

PHP keeps a reference count for all variables and destroys them (in most conditions) as soon as this reference count goes to zero. Objects have one internal reference count and the variables themselves (the object references) each have one reference count. When all the object references have been destroyed because their reference coutns have hit 0, the object itself will be destroyed. Example:

$a = new stdclass; //$a zval refcount 1, object refcount 1
$b = $a;           //$a/$b zval refcount 2, object refcount 1
//this forces the zval separation because $b isn't part of the reference set:
$c = &$a;          //$a/$c zval refcount 2 (isref), $b 1, object refcount 2
unset($c);         //$a zval refcount 1, $b 1, object refcount 2
unset($a);         //$b refcount 1, object refcount 1
unset($b);         //everything is destroyed

But consider the following scenario:

class A {
    public $b;
}
class B {
    public $a;
}

$a = new A;
$b = new B;
$a->b = $b;
$b->a = $a;
unset($a); //cannot destroy object $a because $b still references it
unset($b); //cannot destroy object $b because $a still references it

These cyclic references are where PHP 5.3's garbage collector kicks in. You can explicitly invoke the garbage collector with gc_collect_cycles.

See also Reference Counting Basics and Collecting Cycles in the manual.

这篇关于PHP脚本运行时垃圾收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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