php garbage collection while script running(脚本运行时php垃圾收集)
问题描述
我有一个在 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.
有什么建议吗?
推荐答案
关键是你取消设置你的全局只要您不需要变量,就立即使用它们.
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垃圾收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:脚本运行时php垃圾收集
基础教程推荐
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- HTTP 与 FTP 上传 2021-01-01
- 使用 PDO 转义列名 2021-01-01
- PHP 守护进程/worker 环境 2022-01-01