我有一个在 cron 上运行的 PHP 脚本,最多可能需要 15 分钟才能执行.每隔一段时间,我就会让它吐出 memory_get_usage(),这样我就可以看到发生了什么.它第一次告诉我我的使用量是 10 兆.当脚本完成时,我的体重是 114 兆!
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!
PHP 是否在脚本运行时进行垃圾收集?或者所有这些记忆发生了什么?我可以做些什么来强制垃圾收集.我的脚本正在执行的任务是每晚将几千个节点导入 Drupal.所以它经常做同样的事情.
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.
有什么建议吗?
关键是你 unset 你的全局变量,只要你不需要它们.
The key is that you unset your global variables as soon as you don't need them.
您不需要为局部变量和对象属性显式调用 unset,因为当函数超出范围或对象被销毁时,它们会被销毁.
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 为所有变量保留一个引用计数,并在该引用计数变为零时立即销毁它们(在大多数情况下).对象有一个内部引用计数,而变量本身(对象引用)每个都有一个引用计数.当所有的对象引用都因为引用计数达到 0 而被销毁时,对象本身将被销毁.示例:
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
但请考虑以下情况:
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
这些循环引用是 PHP 5.3 的垃圾收集器发挥作用的地方.您可以使用 gc_collect_cycles 显式调用垃圾收集器
.
These cyclic references are where PHP 5.3's garbage collector kicks in. You can explicitly invoke the garbage collector with gc_collect_cycles
.
另请参阅参考计数基础和收集周期.
这篇关于脚本运行时的php垃圾收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!