(PHP 7 >= 7.3.0, PHP 8)
gc_status — 取得垃圾回收器的資訊
此函式沒有參數。
返回一個包含以下元素的關聯式陣列
"runs"
"collected"
"threshold"
"roots"
"running"
"protected"
"full"
"buffer_size"
"application_time"
"collector_time"
"destructor_time"
"free_time"
版本 | 說明 |
---|---|
8.3.0 |
gc_status() 現在會回傳以下額外欄位:"running" 、"protected" 、"full" 、"buffer_size" 、"application_time" 、"collector_time" 、"destructor_time" 和 "free_time" 。 |
範例 #1 gc_status() 用法
<?php
// 建立需要垃圾回收的物件樹
$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) }