本文介绍了什么gc_collect_cycles函数有用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以在什么情况下解释函数可以有用吗?在大量内存利用率即将发生之前应该调用它吗?解决方案

PHP默认启用垃圾收集器。它用于释放垃圾使用的内存。
gc_collect_cycles()强制收集任何现有的垃圾循环。它返回收集(释放)周期数(对象,变量值...)。启用垃圾收集器不时在内部调用此函数以释放资源。在大多数情况下,PHP脚本的生命期很短。在这种情况下,所有的垃圾都将在工作结束时被销毁,而无需任何垃圾回收。



有时需要手动管理GC:


  1. gc_disable()可以加速一些长操作,但也会导致一些内存开销。 $ b
  2. gc_collect_cycles()可用于指定GC的正确时刻。

使用 gc_collect_cycles() - 调试的另一个原因。假设您想知道 memory_get_usage()的某些代码块的内存消耗情况。你需要先禁用GC,否则你会得到错误的结果。之后,你想分开GC和你的应用程序所消耗的时间。所以,在前后调用 gc_collect_cycles()并测量时间/内存。 小例子 strong>

  class A {
public $ ref;
public $ name;

public function __construct($ name){
$ this-> name = $ name;
echo($ this-> name .'-> __ construct();'。PHP_EOL);


public function __destruct(){
echo($ this-> name .'-> __ destruct();'。PHP_EOL);
}
}

gc_disable();

$ a1 = new A('$ a1');
$ a2 = new A('$ a2');

$ a1-> ref = $ a2;
$ a2-> ref = $ a1;

$ b = new A('$ b');
$ b-> ref = $ a1;

echo('$ a1 = $ a2 = $ b = NULL;'。PHP_EOL);
$ a1 = $ a2 = $ b = NULL;
echo('gc_collect_cycles();'。PHP_EOL);
echo('// removed cycles:'.gc_collect_cycles()。PHP_EOL);
echo('exit();'。PHP_EOL);

会输出:

  $ A1-> __构建体(); 
$ a2-> __ construct();
$ b-> __ construct();
$ a1 = $ a2 => $ b = NULL;
$ b-> __ destruct();
gc_collect_cycles();
$ a2-> __ destruct();
$ a1-> __ destruct();
//删除循环:4

这意味着只有 $ b 在被问到时被销毁。其他 $ a1 $ a2 有循环引用,它是 name 属性也会消耗内存。两个对象+两个字符串= 4(由 gc_collect_cycles()删除。


Can someone please explain under what circumstances gc_collect_cycles function can be useful? Should it be called before a substantial memory utilization is about to take place?

解决方案

PHP has "Garbage Collector" enabled by default. It is used to free memory used by "garbage".gc_collect_cycles() forces collection of any existing garbage cycles. It returns number of collected (freed) cycles (objects, variable values ...). Enabled Garbage Collector calls this function internally from time to time to free resources. In most cases PHP script lives very short time. In this case all garbage will be destroyed in the end of work without any garbage collection.

Sometimes it's needed to manage GC manually:

  1. gc_disable() can speed up some long operations, but also results in some memory overheads.
  2. gc_collect_cycles() could be used to specify the right moments of GC.

Another one reason to use gc_collect_cycles() - debugging. Assume, you want to know what is the memory consumption of some block of code with memory_get_usage(). You need to disable GC first, elsewhere you'll get wrong results. After that you want to separate the time consumed by GC and by your application. So call gc_collect_cycles() and measure timings/memory before and after.

Little example:

class A {
  public $ref;
  public $name;

  public function __construct($name) {
    $this->name = $name;
    echo($this->name.'->__construct();'.PHP_EOL);
  }

  public function __destruct() {
    echo($this->name.'->__destruct();'.PHP_EOL);
  }
}

gc_disable();

$a1 = new A('$a1');
$a2 = new A('$a2');

$a1->ref = $a2;
$a2->ref = $a1;

$b = new A('$b');
$b->ref = $a1;

echo('$a1 = $a2 = $b = NULL;'.PHP_EOL);
$a1 = $a2 = $b = NULL;
echo('gc_collect_cycles();'.PHP_EOL);
echo('// removed cycles: '.gc_collect_cycles().PHP_EOL);
echo('exit();'.PHP_EOL);

Will output:

$a1->__construct();
$a2->__construct();
$b->__construct();
$a1 = $a2 => $b = NULL;
$b->__destruct();
gc_collect_cycles();
$a2->__destruct();
$a1->__destruct();
// removed cycles: 4

This means that only $b was destroyed when it was asked. Other $a1 and $a2 has cyclic references, and it's name properties also consume memory. Two objects + two strings = 4 (removed by gc_collect_cycles()).

这篇关于什么gc_collect_cycles函数有用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 09:35