{
    "slug": "linux_memory_management",
    "term": "Linux Memory Management",
    "category": "linux",
    "difficulty": "intermediate",
    "short": "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.",
    "long": "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.",
    "aliases": [
        "OOM killer",
        "swap",
        "page cache",
        "memory_limit",
        "OOM"
    ],
    "tags": [
        "linux",
        "php",
        "performance",
        "devops"
    ],
    "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."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "php_fpm",
        "linux_performance_tools",
        "systemd_services"
    ],
    "prerequisites": [
        "memory_management",
        "linux_processes",
        "php_fpm"
    ],
    "refs": [
        "https://www.kernel.org/doc/Documentation/filesystems/proc.txt"
    ],
    "bad_code": "; php-fpm.conf — too many workers for available RAM:\npm = static\npm.max_children = 100    ; 100 * 50MB = 5GB needed\n; Server has 4GB RAM\n; At full load: OOM killer fires, workers killed mid-request\n; 500 errors, partial database writes, data corruption",
    "good_code": "; Calculate safely:\n; Available RAM: 4GB - 500MB OS - 500MB MySQL = 3GB for PHP\n; Average worker memory: 50MB\n; Max children: 3000MB / 50MB = 60 workers\n; Leave headroom: use 48\npm = dynamic\npm.max_children = 48\npm.start_servers = 10\npm.min_spare_servers = 5\npm.max_spare_servers = 15\n\n# Monitor OOM kills:\ndmesg | grep -i oom\ngrep -i oom /var/log/syslog",
    "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",
    "severity": "high",
    "effort": "medium",
    "created": "2026-03-16",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/linux_memory_management",
        "html_url": "https://codeclaritylab.com/glossary/linux_memory_management",
        "json_url": "https://codeclaritylab.com/glossary/linux_memory_management.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Linux Memory Management](https://codeclaritylab.com/glossary/linux_memory_management) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/linux_memory_management"
            }
        }
    }
}