gc_collect_cycles

gc_collect_cycles(): int

Forces collection of cyclic garbage and returns the number of objects collected. PHP memory management is refcounting plus periodic cycle detection; this function triggers one detection-and-sweep round immediately.

C C23

C23
#include <stdio.h>
#include <stdlib.h>

typedef struct Node { struct Node *other; } Node;

int main(void) {
    /* C has no GC at all: malloc/free is manual, cycles are your own problem */
    Node *a = malloc(sizeof(Node));
    Node *b = malloc(sizeof(Node));
    a->other = b;
    b->other = a; /* cycle - C never frees; break it before free() */
    free(a);
    free(b); /* safe only because we kept both pointers */
    printf("no GC: malloc/free manual; a cycle means you must break it yourself\n");
    return 0;
}
Output
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++: no tracing GC - a shared_ptr cycle leaks; weak_ptr is the fix
    auto a = std::make_shared<Node>();
    auto b = std::make_shared<Node>();
    a->other = b;
    b->other = a; // cycle: both use counts stay at 2
    std::cout << "no tracing GC: a shared_ptr cycle leaks; weak_ptr breaks it\n";
    return 0;
}
Output
no tracing GC: a shared_ptr cycle leaks; weak_ptr breaks it

C# v11.0.100

v11.0.100
// C#: tracing GC - cycles are invisible; GC.Collect() exists but is rarely needed
var a = new Dictionary<string, object?>();
var b = new Dictionary<string, object?>();
a["other"] = b;
b["other"] = a; // cycle - invisible to a tracing collector
GC.Collect();
Console.WriteLine("tracing GC: GC.Collect() exists, cycles need no help");
Output
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: tracing GC - cycles are invisible and there is no manual collect API
	a := &node{}
	b := &node{}
	a.other = b
	b.other = a // cycle - a tracing collector does not care
	runtime.GC() // only a hint that a sweep would be welcome NOW
	fmt.Println("tracing GC: runtime.GC() is a hint, cycles need no help")
}
Output
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: tracing GC - cycles are invisible; System.gc() is only a hint
        var a = new java.util.HashMap<String, Object>();
        var b = new java.util.HashMap<String, Object>();
        a.put("other", b);
        b.put("other", a); // cycle - invisible to a tracing collector
        System.gc();
        System.out.println("tracing GC: System.gc() is a hint, cycles need no help");
    }
}
Output
tracing GC: System.gc() is a hint, cycles need no help

JavaScript v26.13.1

v26.13.1
// V8 is a tracing GC: cycles are collected automatically, there is no manual API
const a = { tag: "a" };
const b = { tag: "b" };
a.other = b;
b.other = a; // a cycle - invisible to a tracing collector
console.log("tracing GC: cycles collected automatically, no API to call");
Output
tracing GC: cycles collected automatically, no API to call

PHP v8.6.0

v8.6.0
// PHP frees objects by refcounting; a reference cycle needs the cycle collector
function makeGarbage(): void {
    $a = new stdClass();
    $b = new stdClass();
    $a->other = $b;
    $b->other = $a; // cycle: neither refcount ever reaches 0
}
makeGarbage();
echo "collected: ", gc_collect_cycles(), "\n";
makeGarbage();
makeGarbage();
echo "collected: ", gc_collect_cycles(), "\n";
Output
collected: 2
collected: 4

Python v3.15.2

v3.15.2
# Python: refcounting + a cycle collector, just like PHP - and it is also callable
import gc
gc.collect()  # warmup: drain interpreter-startup garbage (count is environment-dependent)
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())
Output
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: no GC at all - an Rc cycle leaks until the process ends; break it with 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)); // cycle: both strong counts stay at 2
    println!("no GC: an Rc cycle leaks; a Weak reference breaks it");
}
Output
no GC: an Rc cycle leaks; a Weak reference breaks it

TypeScript v26.13.1

v26.13.1
// TS: same engine as JS - tracing GC, cycles handled without any API
interface Box { tag: string; other?: Box }
const a: Box = { tag: "a" };
const b: Box = { tag: "b" };
a.other = b;
b.other = a; // a cycle - invisible to a tracing collector
console.log("tracing GC: cycles collected automatically, no API to call");
Output
tracing GC: cycles collected automatically, no API to call

Language differences

C: no GC - malloc/free is manual; a cycle is never freed - break it before free().

C++: no tracing GC - a shared_ptr cycle leaks; weak_ptr breaks it (RAII handles single objects, not cycles).

C#: tracing (generational) GC - cycles are invisible; GC.Collect() exists but is rarely needed.

Go: tracing (concurrent) GC - cycles are invisible; runtime.GC() is only a hint.

Java: tracing (generational) GC - cycles are invisible; System.gc() is only a hint.

JavaScript: V8 is a tracing GC - cycles are reclaimed automatically and there is NO manual API (--expose-gc's globalThis.gc is debugging only).

PHP: refcounting comes first - a cycle like $a->other=$b; $b->other=$a never hits zero; the cycle GC runs automatically on memory thresholds, and gc_collect_cycles() sweeps on demand and returns the collected count; gc_disable()/gc_enable()/gc_status() complete the toolkit; as of PHP 8.5 the return value no longer counts strings and resources collected indirectly.

Python: same lineage as PHP - refcounting plus the gc module's cycle detector; gc.collect() sweeps on demand (the demo counts match PHP: each two-object cycle counts 2).

Rust: no GC - ownership decides lifetime at compile time; break Rc cycles with Weak.

TypeScript: same as JavaScript.