PHP 8.5.0 Alpha 2 available for testing

gc_collect_cycles

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

gc_collect_cyclesForces collection of any existing garbage cycles

Descrizione

gc_collect_cycles(): int

Forces collection of any existing garbage cycles.

Elenco dei parametri

Questa funzione non contiene parametri.

Valori restituiti

Returns number of collected cycles.

Vedere anche:

add a note

User Contributed Notes 2 notes

up
199
Mason
13 years ago
(Since this function wasn't documented as of the date I left this note...)

If you came here looking for documentation, allow me to point you instead to a section in the user manual about garbage collection that includes a bit at the end about when you would use this feature and what it will actually do: http://php.net/manual/en/features.gc.collecting-cycles.php

Hope it helps!
up
1
ivijan dot stefan at gmail dot com
12 hours ago
<?php gc_collect_cycles(); ?> triggers PHP's garbage collector to explicitly search for and clean up circular references - objects that reference each other but are no longer accessible by the program.

Although PHP's garbage collector runs automatically, in long-running scripts or memory-intensive operations (such as daemons, CLI tools, or large loops), calling this function manually can help reduce memory usage and prevent memory leaks.

Example:

<?php

class Node {
public
$ref;
}

$a = new Node();
$b = new Node();

$a->ref = $b;
$b->ref = $a;

unset(
$a, $b);

// At this point, $a and $b are no longer accessible, but the circular reference remains in memory.
// Force cleanup:
$collected = gc_collect_cycles();

echo
"Collected cycles: $collected\n";
?>
To Top