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

Out of Memory Errors (memory_limit)

php PHP 5.0+ Intermediate
debt(d7/e5/b5/t7)
d7 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'only careful code review or runtime testing' (d7), profilers like blackfire/xdebug can reveal memory issues but no default linter flags file_get_contents on large files; often only discovered under load.

e5 Effort Remediation debt — work required to fix once spotted

Closest to 'touches multiple files / significant refactor in one component' (e5), per quick_fix replacing file_get_contents with streaming/generators usually means refactoring data processing logic across the import/export component, not a one-line swap.

b5 Burden Structural debt — long-term weight of choosing wrong

Closest to 'persistent productivity tax' (b5), applies across web/cli/queue contexts and shapes how data-handling code must be written everywhere large datasets appear.

t7 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'serious trap' (t7), the misconception explicitly states devs believe raising memory_limit fixes OOM when it only defers it — contradicts the intuitive 'give it more memory' fix.

About DEBT scoring →

TL;DR

PHP enforces memory_limit in php.ini — exceeding it triggers a fatal E_ERROR that cannot be caught with set_error_handler().

Explanation

memory_limit defaults to 128M (256M in recent distros). When exceeded, PHP emits a fatal error and terminates. It cannot be caught normally but register_shutdown_function() + error_get_last() can detect it. Common causes: loading large files into strings, unbounded array growth, processing huge CSV/Excel imports, missing pagination on DB queries. Increase with ini_set('memory_limit','512M') only for the specific operation, then restore. Use generators and streaming to avoid loading everything into memory. memory_get_peak_usage(true) reports peak consumption.

Common Misconception

Increasing memory_limit fixes OOM errors — it only defers them. The real fix is streaming/chunking to avoid loading everything into memory.

Why It Matters

OOM errors crash the request silently in production if not detected via shutdown functions — proper streaming and chunking prevents them entirely.

Common Mistakes

  • Setting memory_limit = -1 in production — removes the safety net.
  • Not using generators for large dataset iteration.
  • Loading entire files with file_get_contents() when streaming suffices.
  • Not monitoring peak memory usage.

Code Examples

✗ Vulnerable
// Loading a 500MB CSV into memory
$data = file_get_contents('/exports/huge.csv');
$rows = explode("\n", $data); // Fatal: Allowed memory size exhausted
✓ Fixed
// Stream line by line — constant memory usage
$handle = fopen('/exports/huge.csv', 'r');
while (($line = fgets($handle)) !== false) {
    processLine($line);
}
fclose($handle);

// Or with generators
function readCsv(string $file): Generator {
    $h = fopen($file, 'r');
    while (($row = fgetcsv($h)) !== false) yield $row;
    fclose($h);
}

Added 22 Mar 2026
Views 32
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 0 pings F 1 ping S 0 pings S 1 ping M 0 pings T 1 ping W 0 pings T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 2 pings T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F
No pings yet today
No pings yesterday
Amazonbot 7 Perplexity 6 Unknown AI 3 Google 3 SEMrush 3 ChatGPT 1 Meta AI 1 Ahrefs 1
crawler 22 crawler_json 1 pre-tracking 2
DEV INTEL Tools & Severity
🟠 High ⚙ Fix effort: Medium
⚡ Quick Fix
Stream large data with fgets()/fgetcsv() or generators instead of loading into memory. Monitor with memory_get_peak_usage(). Set memory_limit per-request, not globally.
📦 Applies To
PHP 5.0+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
file_get_contents\|memory_limit
Auto-detectable: ✗ No blackfire xdebug
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Medium ✗ Manual fix Fix: Medium Context: Function Tests: Update
CWE-400 CWE-770

✓ schema.org compliant