How does the PHP garbage collection process handle cyclic references, and what changes have been made to improve memory management in recent PHP versions?

Cyclic references in PHP can cause memory leaks because the garbage collector may not be able to properly deallocate memory when objects reference each other in a loop. To solve this issue, PHP introduced a garbage collection mechanism that can detect and break cyclic references to free up memory.

class Node {
    public $next;

    public function __construct() {
        $this->next = null;
    }
}

// Creating cyclic references
$node1 = new Node();
$node2 = new Node();
$node1->next = $node2;
$node2->next = $node1;

// Breaking the cyclic reference
$node1->next = null;
$node2->next = null;