<?php declare(strict_types=1); namespace Repro; class CycleValue { private ?\stdClass $cycleRef; public function __construct() { // make self cyclically referenced $this->cycleRef = new \stdClass(); $this->cycleRef->x = $this; } public function __destruct() { echo "d\n"; $this->cycleRef->x = null; $this->cycleRef = null; } } class CycleWithDestructorForGcMonitoring { private \Closure $destructorFx; private \stdClass $cycleRef; public function __construct(\Closure $destructorFx) { $this->destructorFx = $destructorFx; $this->cycleRef = new \stdClass(); $this->cycleRef->x = $this; } public function __destruct() { ($this->destructorFx)(); } } $ref = \WeakReference::create(new CycleValue()); $gcRuns = 0; $isSecondGcRerun = false; // https://github.com/php/php-src/commit/b58d74547f $createMonitorGcFx = static function () use (&$createMonitorGcFx, &$gcRuns, &$isSecondGcRerun, $ref): void { $destructorFx = static function () use (&$createMonitorGcFx, &$gcRuns, &$isSecondGcRerun, $ref): void { $gcRunsPrev = $gcRuns; $gcRuns = gc_status()['runs']; if ($gcRunsPrev !== $gcRuns) { // prevent recursion on shutdown echo "gc" . ($isSecondGcRerun ? ' rerun' : '') . "\n"; var_dump($ref->get() !== null); $isSecondGcRerun = !$isSecondGcRerun; $createMonitorGcFx(); } }; new CycleWithDestructorForGcMonitoring($destructorFx); }; $createMonitorGcFx(); gc_collect_cycles(); /**/if (PHP_VERSION_ID < 80100) { gc_collect_cycles(); } var_dump($ref->get() !== null); echo "done\n";
You have javascript disabled. You will not be able to edit any code.