Home / News / PHP GC with Example

PHP GC with Example

Let’s break down PHP Garbage Collection (GC) and Memory Management in a very simple way with some sarcastic examples:

🧠 What is Memory Management in PHP?

When your PHP script runs, it needs memory (RAM) to:

  • Store variables
  • Keep track of arrays/objects
  • Run functions

PHP automatically allocates memory when you create variables and freees it when those variables are no longer needed.

👉 When a variable goes out of scope, PHP automatically frees its memory:

  • Normal variable cleanup: gc_collect_cycles (rarely needed manually)
  • Circular references: gc_collect_cycles (forces GC manually (used only in rare debugging/performance cases))
  • Memory usage: memory_get_usage / memory_get_peak_usage (useful for monitoring memory)

Let’s break down PHP Garbage Collection (GC) and Memory Management in a very simple way with examples.

🧠 What is Memory Management in PHP?

When your PHP script runs, it needs memory (RAM) to:

  • Store variables
  • Keep track of arrays/objects
  • Run functions

PHP automatically allocates memory when you create variables and frees it when those variables are no longer needed.

🗑️ What is Garbage Collection (GC)?

Garbage Collection is the process of cleaning up memory that’s no longer in use.

👉 PHP normally frees memory when variables go out of scope.
👉 But with circular references (when objects reference each other), PHP can’t automatically detect that memory should be freed. This is where GC steps in.

🔹 Example 1: Normal Variable Cleanup (No Garbage Collector Needed)

<?php
function test() {
    $name = "Ahmed";  // PHP allocates memory
    echo $name;       // "Ahmed"
} // <- After function ends, $name is destroyed, memory is freed

test();
?>

➡️ Here, $name is automatically freed after the function ends.
GC is not needed.

🔹 Example 2: Circular Reference (Needs Garbage Collection)

<?php
class A {
    public $ref;
}

$a = new A();
$b = new A();

$a->ref = $b;
$b->ref = $a;

// Now $a and $b reference each other (circular reference)

// Unset variables
unset($a, $b);

// 🚨 Memory is still held because $a <-> $b reference each other!
// PHP’s Garbage Collector detects this and frees it later.
?>

➡️ Without GC, this memory would stay allocated until the script ends (a memory leak).
➡️ With GC, PHP notices this cycle and frees it.

🔹 Example 3: Checking Memory Usage

<?php
echo "Initial: " . memory_get_usage() . " bytes\n";

$a = str_repeat("Hello", 1000000); // Big string

echo "After allocation: " . memory_get_usage() . " bytes\n";

unset($a); // Free variable

echo "After unset: " . memory_get_usage() . " bytes\n";

// Force garbage collection (rarely needed manually)
gc_collect_cycles();

echo "After GC: " . memory_get_usage() . " bytes\n";
?>

➡️ Output will show memory increases when $a is created, decreases after unset(), and drops more after gc_collect_cycles() if cycles were present.

⚡ Key Points

  1. Normal memory cleanup: Happens when variables go out of scope.
  2. Circular references: Need Garbage Collection.
  3. gc_collect_cycles(): Forces GC manually (used only in rare debugging/performance cases).
  4. memory_get_usage() / memory_get_peak_usage(): Useful for monitoring memory.

✅ In short:

  • PHP handles memory for you.
  • GC mainly exists to handle circular references.
  • Most of the time, you don’t worry about it unless you’re writing long-running scripts (like daemons, workers, or cron jobs).
Tagged:

Leave a Reply

Your email address will not be published. Required fields are marked *