← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Memory Management — Stack vs Heap

compiler PHP 5.0+ Intermediate

Also Known As

stack memory heap memory garbage collection ZendMM

TL;DR

The stack holds function call frames with fixed-size local variables — fast, automatic, limited. The heap holds dynamically allocated objects — flexible, managed by GC, slower.

Explanation

Stack memory: allocated at function call, freed at return. Stores: local variables, function parameters, return addresses. Fixed size (typically 8MB on Linux). Extremely fast — just a pointer increment. Stack overflow: too many nested calls exhaust the stack. Heap memory: allocated with new/malloc, freed by garbage collection or explicit free. Stores: objects, arrays, large data structures. PHP uses a reference-counting GC with cycle collector. ZendMM (Zend Memory Manager) manages PHP's heap. PHP does not expose stack vs heap to the developer — all variables are reference-counted heap values by default.

Common Misconception

PHP developers don't need to understand memory management — PHP's GC handles allocation, but developers cause memory issues through circular references, large object accumulation, and not using generators for large datasets.

Why It Matters

PHP processes that accumulate objects in long-running scripts (queue workers, CLI commands) can exhaust memory — understanding reference counting explains why circular references cause leaks and why generators save memory.

Common Mistakes

  • Circular references without unset() — PHP's refcount GC handles most cycles but cyclic destruction order can cause leaks.
  • Appending to a huge array in a loop instead of using generators.
  • Not calling unset() on large variables in long-running scripts.
  • Using PHP's GC with long-lived objects — gc_collect_cycles() can be called manually for cycle collection.

Code Examples

✗ Vulnerable
// Memory leak via circular reference:
class Node {
    public ?Node $parent = null;
    public array $children = [];
}
$parent = new Node();
$child  = new Node();
$parent->children[] = $child;
$child->parent      = $parent; // Circular: parent→child→parent
unset($parent, $child); // Refcount never reaches 0 — leak!
// PHP cycle GC eventually collects these, but slowly
✓ Fixed
// Break circular references explicitly:
unset($child->parent); // Break cycle before unset
unset($parent, $child); // Now refcount reaches 0 immediately

// Or use WeakReference for back-references:
class Node {
    public ?\WeakReference $parent = null; // Does not increment refcount
}
$child->parent = \WeakReference::create($parent);
// When $parent is unset, WeakReference returns null — no cycle

Added 16 Mar 2026
Edited 22 Mar 2026
Views 49
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 1 ping S 1 ping M 0 pings T 0 pings W 0 pings T 2 pings F 3 pings S 0 pings S 0 pings M 0 pings T 1 ping W 1 ping T 1 ping F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T
No pings yet today
No pings yesterday
Amazonbot 14 Perplexity 10 Google 6 Unknown AI 4 Ahrefs 3 SEMrush 2 ChatGPT 1 Majestic 1
crawler 39 crawler_json 1 pre-tracking 1
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Medium
⚡ Quick Fix
Monitor memory_get_peak_usage() in production; use generators for large datasets; unset large variables when done; use PHP 7+ which halved memory usage vs PHP 5
📦 Applies To
PHP 5.0+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
memory_limit hit in production; loading entire CSV database result into array; 100k+ objects in memory simultaneously
Auto-detectable: ✓ Yes blackfire php-meminfo datadog
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Medium ✗ Manual fix Fix: Medium Context: File
CWE-401 CWE-400

✓ schema.org compliant