Garbage Collection and Cyclic References
PHP primarily relies on reference counting to manage memory. When the reference count of a value drops to zero, PHP frees the associated memory immediately. However, this mechanism fails in one important case: cyclic references.
A cyclic reference occurs when two or more variables reference each other in a loop. Even if you unset all external variables, the objects still reference one another, so their reference counts never reach zero. As a result, PHP cannot free this memory using reference counting alone.
To solve this problem, the Zend Engine includes a garbage collector that periodically scans memory for these unreachable cycles. When a cycle is detected, the garbage collector safely frees the memory, preventing silent memory leaks in long-running scripts.
The following example creates a cyclic reference and then forces PHP's garbage collector to clean it up.
cyclic_references.php
12345678910111213141516171819202122232425262728<?php // Example: Cyclic references and garbage collection in PHP class Node { public $child; } // Create two objects that reference each other (cycle) $a = new Node(); $b = new Node(); $a->child = $b; $b->child = $a; // At this point, $a and $b reference each other. // Unset the variables to remove external references. unset($a, $b); // Reference counts for the objects are not zero due to the cycle. // Without garbage collection, this memory would not be freed. // Force garbage collection to collect cycles $collected = gc_collect_cycles(); // Output the number of collected cycles echo "Garbage collector collected $collected cycle(s).\n"; // When garbage collection runs, it detects the cycle and frees memory. ?>
The table below summarizes what happens during each stage of the script and how PHP's memory manager responds.
¡Gracias por tus comentarios!
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla
Genial!
Completion tasa mejorada a 11.11
Garbage Collection and Cyclic References
Desliza para mostrar el menú
PHP primarily relies on reference counting to manage memory. When the reference count of a value drops to zero, PHP frees the associated memory immediately. However, this mechanism fails in one important case: cyclic references.
A cyclic reference occurs when two or more variables reference each other in a loop. Even if you unset all external variables, the objects still reference one another, so their reference counts never reach zero. As a result, PHP cannot free this memory using reference counting alone.
To solve this problem, the Zend Engine includes a garbage collector that periodically scans memory for these unreachable cycles. When a cycle is detected, the garbage collector safely frees the memory, preventing silent memory leaks in long-running scripts.
The following example creates a cyclic reference and then forces PHP's garbage collector to clean it up.
cyclic_references.php
12345678910111213141516171819202122232425262728<?php // Example: Cyclic references and garbage collection in PHP class Node { public $child; } // Create two objects that reference each other (cycle) $a = new Node(); $b = new Node(); $a->child = $b; $b->child = $a; // At this point, $a and $b reference each other. // Unset the variables to remove external references. unset($a, $b); // Reference counts for the objects are not zero due to the cycle. // Without garbage collection, this memory would not be freed. // Force garbage collection to collect cycles $collected = gc_collect_cycles(); // Output the number of collected cycles echo "Garbage collector collected $collected cycle(s).\n"; // When garbage collection runs, it detects the cycle and frees memory. ?>
The table below summarizes what happens during each stage of the script and how PHP's memory manager responds.
¡Gracias por tus comentarios!