Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Concepts and Reference

PHP memory and garbage collection

How PHP manages memory: reference counting, copy-on-write, the cycle collector, destructors, weak references, memory_get_usage and memory_limit explained.

PHP manages memory for you. Every value has a reference count: the number of variables, array slots and properties that point at it. When the count drops to zero, PHP frees the value immediately. Because most scripts live for a single request, memory problems show up mainly in long-running scripts, queue workers and data imports.

Two details explain most behavior: copy-on-write (assigning an array does not copy it until one side changes) and the cycle collector (objects that reference each other never reach a count of zero, so a separate pass finds them).

PHP
<?php
$a = [1, 2, 3];
$b = $a;       // no copy yet: both names share one array
$b[] = 4;      // now $b gets its own copy

Try it

PHP Destructors show when the reference count hits zero

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Cycles and gc_collect_cycles()

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Generators keep memory flat

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Useful functions and settings

Item Purpose
memory_get_usage(bool $real = false) Bytes currently used by the script.
memory_get_peak_usage(bool $real = false) Highest usage so far.
memory_limit (ini) Maximum per script. -1 means unlimited. Exceeding it is a fatal error.
gc_collect_cycles() Runs the cycle collector now and returns how many values it freed.
gc_enable(), gc_disable(), gc_enabled() Switch the automatic cycle collector.
gc_status() Collector statistics (runs, collected, threshold).
unset($var), $var = null Drop a reference.
WeakReference, WeakMap Refer to objects without keeping them alive.

Usage notes

  • The cycle collector runs automatically when a buffer of possible roots fills up (10,000 by default). You rarely need to call it by hand, except in long workers after large batches.
  • Memory is not always returned to the operating system when PHP frees it; the engine reuses it for later allocations.
  • Objects are handles: assigning an object copies the handle, not the object. Use clone for a copy.
  • Reading a big file into a string or an array is the usual cause of memory errors. Stream it (fgets(), SplFileObject) or use generators.
  • Memory numbers differ between PHP versions and platforms, so never hard-code them in tests. Compare them instead.
  • Long-running scripts should limit growth: clear caches, avoid static arrays that only grow, and restart workers periodically.