Content · Programming · PHP

PHP gc_collect_cycles() Function: Usage and Examples

Understand PHP gc_collect_cycles() with three examples, return behavior, important pitfalls, and related operations.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

Use gc_collect_cycles() when you need to runs the cycle collector and returns the number of cycles collected in a PHP application. The examples below show the actual operation and its limitations.

What Is gc_collect_cycles() in PHP?

Runs the cycle collector and returns the number of cycles collected. The exact signature supported by your installation depends on its PHP version and enabled extensions.

Its syntax is:

gc_collect_cycles(): int

Parameters and return value: The parameter types, optional arguments, references, and return type are shown in the signature above. Optional parameters are marked with an ellipsis in this reference signature.

Example 1: Basic Usage

<?php

echo gc_collect_cycles();

Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.

Example 2: Different Input or Behavior

<?php

gc_enable(); echo gc_collect_cycles();

Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.

Practical Example: Applying the Function

<?php

class LoopNode { public ?LoopNode $next = null; } $node = new LoopNode(); $node->next = $node; unset($node); echo gc_collect_cycles();

Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.

Important Behavior and Edge Cases

Only cyclic garbage is targeted; the number collected depends on runtime state.

Comparison With Related Functions

ApproachDifference
gc_collect_cycles()Runs the cycle collector and returns the number of cycles collected.
Related operationgc_status() reports collector statistics; gc_collect_cycles() triggers a collection pass.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring the function-specific limitationOnly cyclic garbage is targeted; the number collected depends on runtime state.
Confusing this function with a related operationgc_status() reports collector statistics; gc_collect_cycles() triggers a collection pass.

Key Takeaways

  • Runs the cycle collector and returns the number of cycles collected.
  • Only cyclic garbage is targeted; the number collected depends on runtime state.
  • gc_status() reports collector statistics; gc_collect_cycles() triggers a collection pass.

Use gc_collect_cycles() for the specific operation demonstrated above. Check its prerequisites and handle failure conditions before using it with production data.

Share this resourceSend this page to someone who may find it useful.