gc_collect_cycles
gc_collect_cycles(): int
强制回收循环引用垃圾并返回本次收集的对象数。PHP 内存管理 = 引用计数为主 + 定期环检测;此函数立即触发一轮检测清扫。
C C23
C23
#include <stdio.h>
#include <stdlib.h>
typedef struct Node { struct Node *other; } Node;
int main(void) {
/* C 完全没有 GC:malloc/free 全手动,环是你自己的问题 */
Node *a = malloc(sizeof(Node));
Node *b = malloc(sizeof(Node));
a->other = b;
b->other = a; /* 成环——C 从不自动释放;free() 前必须先破环 */
free(a);
free(b); /* 仅因为我们保留了两个指针才安全 */
printf("no GC: malloc/free manual; a cycle means you must break it yourself\n");
return 0;
}
输出
no GC: malloc/free manual; a cycle means you must break it yourself
C++ C++26
C++26
#include <iostream>
#include <memory>
struct Node { std::shared_ptr<Node> other; };
int main() {
// C++:没有追踪式 GC——shared_ptr 成环会泄漏;weak_ptr 是解法
auto a = std::make_shared<Node>();
auto b = std::make_shared<Node>();
a->other = b;
b->other = a; // 成环:两个 use_count 都停在 2
std::cout << "no tracing GC: a shared_ptr cycle leaks; weak_ptr breaks it\n";
return 0;
}
输出
no tracing GC: a shared_ptr cycle leaks; weak_ptr breaks it
C# v11.0.100
v11.0.100
// C#:追踪式 GC——环不可见;GC.Collect() 存在但极少需要
var a = new Dictionary<string, object?>();
var b = new Dictionary<string, object?>();
a["other"] = b;
b["other"] = a; // 成环——对追踪式回收器不可见
GC.Collect();
Console.WriteLine("tracing GC: GC.Collect() exists, cycles need no help");
输出
tracing GC: GC.Collect() exists, cycles need no help
Go v1.27.4
v1.27.4
package main
import (
"fmt"
"runtime"
)
type node struct{ other *node }
func main() {
// Go:追踪式 GC——环不可见,也没有手动回收 API
a := &node{}
b := &node{}
a.other = b
b.other = a // 成环——追踪式回收器不在乎
runtime.GC() // 只是「希望现在清扫」的提示
fmt.Println("tracing GC: runtime.GC() is a hint, cycles need no help")
}
输出
tracing GC: runtime.GC() is a hint, cycles need no help
Java v27.0.1
v27.0.1
public class Main {
public static void main(String[] args) {
// Java:追踪式 GC——环不可见;System.gc() 只是提示
var a = new java.util.HashMap<String, Object>();
var b = new java.util.HashMap<String, Object>();
a.put("other", b);
b.put("other", a); // 成环——对追踪式回收器不可见
System.gc();
System.out.println("tracing GC: System.gc() is a hint, cycles need no help");
}
}
输出
tracing GC: System.gc() is a hint, cycles need no help
JavaScript v26.13.1
v26.13.1
// V8 是追踪式 GC:环会被自动回收,没有手动 API
const a = { tag: "a" };
const b = { tag: "b" };
a.other = b;
b.other = a; // 成环——对追踪式回收器不可见
console.log("tracing GC: cycles collected automatically, no API to call");
输出
tracing GC: cycles collected automatically, no API to call
PHP v8.6.0
v8.6.0
// PHP 靠引用计数释放对象;引用环需要环回收器出手
function makeGarbage(): void {
$a = new stdClass();
$b = new stdClass();
$a->other = $b;
$b->other = $a; // 成环:两个引用计数永远到不了 0
}
makeGarbage();
echo "collected: ", gc_collect_cycles(), "\n";
makeGarbage();
makeGarbage();
echo "collected: ", gc_collect_cycles(), "\n";
输出
collected: 2 collected: 4
Python v3.15.2
v3.15.2
# Python:引用计数 + 环回收器,与 PHP 同源——而且也可以手动调用
import gc
gc.collect() # 预热:排干解释器启动垃圾(其数量随环境变化,必须先清一次)
class Node:
pass
def make_garbage():
a, b = Node(), Node()
a.other, b.other = b, a
make_garbage()
print("collected:", gc.collect())
make_garbage()
make_garbage()
print("collected:", gc.collect())
输出
collected: 2 collected: 4
Rust v1.99.1
v1.99.1
use std::cell::RefCell;
use std::rc::Rc;
#[derive(Debug)]
struct Node {
other: RefCell<Option<Rc<Node>>>,
}
fn main() {
// Rust:完全没有 GC——Rc 成环会泄漏到进程结束;用 Weak 破环
let a = Rc::new(Node { other: RefCell::new(None) });
let b = Rc::new(Node { other: RefCell::new(None) });
*a.other.borrow_mut() = Some(Rc::clone(&b));
*b.other.borrow_mut() = Some(Rc::clone(&a)); // 成环:两个强引用计数都停在 2
println!("no GC: an Rc cycle leaks; a Weak reference breaks it");
}
输出
no GC: an Rc cycle leaks; a Weak reference breaks it
TypeScript v26.13.1
v26.13.1
// TS:与 JS 同引擎——追踪式 GC,环无需任何 API 介入
interface Box { tag: string; other?: Box }
const a: Box = { tag: "a" };
const b: Box = { tag: "b" };
a.other = b;
b.other = a; // 成环——对追踪式回收器不可见
console.log("tracing GC: cycles collected automatically, no API to call");
输出
tracing GC: cycles collected automatically, no API to call
语言差异说明
C:无 GC,malloc/free 手动;成环不释放,先破环再 free。
C++:无追踪式 GC;shared_ptr 环泄漏,weak_ptr 破环(RAII 管单个对象不管环)。
C#:追踪式 GC(分代),环不可见;GC.Collect() 存在但极少需要。
Go:追踪式 GC(并发),环不可见;runtime.GC() 只是提示。
Java:追踪式 GC(分代),环不可见;System.gc() 只是提示。
JavaScript:V8 追踪式 GC,环自动回收,无任何手动 API(--expose-gc 的 globalThis.gc 仅调试用)。
PHP:引用计数为主,$a->other=$b; $b->other=$a 这类环计数永不为 0;环 GC 默认随内存阈值自动触发,gc_collect_cycles() 手动立即清扫并返回收集数;配套 gc_disable()/gc_enable()/gc_status();PHP 8.5 起返回值不再计入被间接收集的字符串与资源。
Python:与 PHP 同宗——引用计数 + gc 模块的环检测,gc.collect() 手动清扫(示例计数与 PHP 一致:每个两对象环收 2)。
Rust:无 GC——所有权在编译期决定生死;Rc 环用 Weak 破。
TypeScript:同 JavaScript。