Linux Memory Management
Also Known As
OOM killer
swap
page cache
memory_limit
OOM
TL;DR
How Linux allocates RAM — virtual memory, the page cache, swap, and the OOM killer — and what PHP developers need to know when PHP processes run out of memory.
Explanation
Linux uses virtual memory — each process sees its own address space. The page cache uses free RAM to cache disk reads (cat /proc/meminfo shows Cached). Swap extends RAM to disk — much slower, causes performance degradation. OOM (Out of Memory) killer: when RAM + swap is exhausted, the kernel kills processes to free memory — typically the largest consumer. PHP memory: each PHP-FPM worker uses 20-100MB depending on app; pm.max_children * typical_worker_memory must not exceed available RAM. Memory pressure indicators: free -h, vmstat, /proc/meminfo. PHP memory limit: memory_limit in php.ini controls per-request limit.
Common Misconception
✗ Free memory is wasted memory — Linux deliberately uses free RAM for the page cache; high memory usage with low free memory is normal and healthy as long as there is no swap usage.
Why It Matters
PHP-FPM with pm.max_children set too high causes RAM exhaustion — the OOM killer terminates PHP workers mid-request, causing 500 errors and data corruption.
Common Mistakes
- pm.max_children calculated without accounting for page cache — leave 20% RAM for cache.
- No swap configured — without swap, OOM killer fires immediately when RAM is exhausted.
- memory_limit = -1 (unlimited) in PHP — uncontrolled memory leaks exhaust server RAM.
- Not monitoring RSS (resident set size) per PHP worker — workers grow over time with memory leaks.
Code Examples
✗ Vulnerable
; php-fpm.conf — too many workers for available RAM:
pm = static
pm.max_children = 100 ; 100 * 50MB = 5GB needed
; Server has 4GB RAM
; At full load: OOM killer fires, workers killed mid-request
; 500 errors, partial database writes, data corruption
✓ Fixed
; Calculate safely:
; Available RAM: 4GB - 500MB OS - 500MB MySQL = 3GB for PHP
; Average worker memory: 50MB
; Max children: 3000MB / 50MB = 60 workers
; Leave headroom: use 48
pm = dynamic
pm.max_children = 48
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 15
# Monitor OOM kills:
dmesg | grep -i oom
grep -i oom /var/log/syslog
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
16 Mar 2026
Edited
22 Mar 2026
Views
34
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Amazonbot 11
Unknown AI 4
Perplexity 4
ChatGPT 3
Ahrefs 3
Google 3
Also referenced
How they use it
crawler 27
pre-tracking 1
⚡
DEV INTEL
Tools & Severity
🟠 High
⚙ Fix effort: Medium
⚡ Quick Fix
Configure vm.swappiness=10 on PHP servers to prefer RAM over swap; monitor /proc/meminfo MemAvailable; PHP-FPM OOM kills mean pm.max_children is too high for available RAM
📦 Applies To
any
web
cli
🔗 Prerequisites
🔍 Detection Hints
PHP-FPM workers OOM-killed; server using swap heavily causing performance degradation; pm.max_children not calculated from available RAM
Auto-detectable:
✓ Yes
datadog
prometheus-node-exporter
free
vmstat
⚠ Related Problems
🤖 AI Agent
Confidence: Low
False Positives: Medium
✗ Manual fix
Fix: High
Context: File