gc_status

(PHP 7 >= 7.3.0, PHP 8)

gc_status获取有关垃圾回收的信息

说明

gc_status(): array

获取有关当前垃圾收集状态的信息。

参数

此函数没有参数。

返回值

返回包含以下元素的关联数组:

  • "runs"
  • "collected"
  • "threshold"
  • "roots"
  • "running"
  • "protected"
  • "full"
  • "buffer_size"
  • "application_time"
  • "collector_time"
  • "destructor_time"
  • "free_time"

更新日志

版本说明
8.3.0gc_status() 现在会返回以下附加字段: "running""protected""full""buffer_size""application_time""collector_time""destructor_time""free_time"

示例

示例 #1 gc_status() 用法

<?php

// create object tree that needs gc collection
$a = new stdClass();
$a->b = [];
for (
$i = 0; $i < 100000; $i++) {
$b = new stdClass();
$b->a = $a;
$a->b[] = $b;
}
unset(
$a);
unset(
$b);
gc_collect_cycles();

var_dump(gc_status());

以上示例的输出类似于:

array(4) { ["runs"]=> int(5) ["collected"]=> int(100002) ["threshold"]=> int(50001) ["roots"]=> int(0) }

上述示例在 PHP 8.3 中的输出类似于于:

array(12) { ["running"]=> bool(false) ["protected"]=> bool(false) ["full"]=> bool(false) ["runs"]=> int(5) ["collected"]=> int(100002) ["threshold"]=> int(50001) ["buffer_size"]=> int(131072) ["roots"]=> int(0) ["application_time"]=> float(0.031182458) ["collector_time"]=> float(0.020106291) ["destructor_time"]=> float(0) ["free_time"]=> float(0.003707167) }
To Top