Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Garbage Collection and Cyclic References | Reference Counting and Copy on Write
Practice
Projects
Quizzes & Challenges
Frågesporter
Challenges
/
PHP Memory Management

bookGarbage 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

cyclic_references.php

copy
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.

question mark

Which statement best describes how PHP's garbage collector handles cyclic references?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 3

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

bookGarbage Collection and Cyclic References

Svep för att visa menyn

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

cyclic_references.php

copy
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.

question mark

Which statement best describes how PHP's garbage collector handles cyclic references?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 3
some-alt