items[$productId])) { return $this->items[$productId]; } $price = new Price($productId, $productId * 1.5); $this->items[$productId] = $price; if (count($this->items) > 60) { unset($this->items[array_key_first($this->items)]); // bounded } return $price; } } class Connection { public ?Connection $peer = null; // a cycle public static function pair(): void { $a = new Connection(); $b = new Connection(); $a->peer = $b; $b->peer = $a; // left to the GC } } class EventBus { /** @var list */ public static array $listeners = []; public static function on(Closure $listener): void { self::$listeners[] = $listener; } } $customers = []; $audit = []; $prices = new PriceCache(); function handleBatch(int $batch, PriceCache $prices, array &$customers, array &$audit): void { for ($i = 0; $i < 80; $i++) { $id = $batch * 80 + $i; $order = new Order($id); // freed $order->lines[] = $prices->get(($id * 7) % 200); SqlLogger::log("INSERT INTO orders (id) VALUES ($id)"); $audit[] = sprintf('%s shipped', 'order'); // copies if ($i % 8 === 0) { $customers[$id] = new Customer($id, "c$id"); // half expire } usleep(4000); // the work } foreach (array_keys($customers) as $k => $id) { if ($k % 2 === 0) { unset($customers[$id]); } } Connection::pair(); } gc_disable(); // cycles stay: orphans for ($k = 0; $k < 12; $k++) { EventBus::on(fn (Order $o) => $o->id + $k); // listeners } cryo_metrics_sample(50, __DIR__ . '/shop.cryom'); for ($batch = 0; $batch < 5; $batch++) { handleBatch($batch, $prices, $customers, $audit); cryo_snapshot(__DIR__ . "/shop-$batch.cryo", ['gc' => false]); cryo_metrics_mark("snapshot shop-$batch"); } cryo_metrics_sample(0, __DIR__ . '/shop.cryom'); echo "5 snapshots and shop.cryom in ", __DIR__, "\n";