Ad Space — Top Banner

Fatal error: Out of memory

PHP Programming Language

Severity: Critical

What Does This Error Mean?

PHP's 'Out of memory' fatal error means the server itself ran out of physical RAM — not just that your PHP memory limit was hit. This is different from the 'Allowed memory size exhausted' error, which is a PHP-level limit. Out of memory means the operating system could not allocate the physical memory PHP requested. This is a more serious problem that usually requires server-level changes.

Affected Models

  • PHP 5.x
  • PHP 7.x
  • PHP 8.x
  • All PHP versions

Common Causes

  • The server is running many PHP processes simultaneously and has run out of RAM
  • A script is allocating enormous amounts of memory — loading huge files or arrays into memory
  • Memory leaks in long-running scripts or PHP-FPM worker processes that are not restarted
  • The server has too little RAM for the workload — commonly seen on small VPS instances
  • A PHP extension or third-party library has a memory leak

How to Fix It

  1. Check the server's available RAM: use 'free -m' on Linux. If RAM is consistently near 100%, you need more memory or fewer processes.

    A 512MB VPS running multiple PHP workers, MySQL, and a web server can run out of RAM under normal load.

  2. Reduce the PHP-FPM worker count to use fewer simultaneous PHP processes. Each process uses 20–50MB of RAM.

    In your PHP-FPM pool config, reduce pm.max_children. Fewer workers means less total RAM used.

  3. Enable PHP opcode caching (OPcache). It dramatically reduces memory usage and CPU load by caching compiled PHP scripts.

    OPcache is included in PHP 5.5+ and just needs to be enabled in php.ini: opcache.enable=1

  4. Profile memory-heavy scripts and optimize them. Use memory_get_peak_usage() to find which scripts use the most memory.

    Replace large in-memory operations with database queries, streaming, or batch processing.

  5. If this happens during Composer operations (installing packages), add swap space to the server. Even 1GB of swap can prevent out-of-memory crashes during installs.

    Swap is slower than RAM but prevents hard crashes when RAM is briefly exceeded.

When to Call a Professional

Out of memory errors at the server level require a system administrator or DevOps engineer. A PHP developer can help optimize the scripts, but if the server is simply underpowered for the load, you need to upgrade the server or optimize the infrastructure. This is not a problem that can be solved by changing a single line of code.

Frequently Asked Questions

What is the difference between 'Allowed memory size exhausted' and 'Out of memory'?

'Allowed memory size exhausted' means PHP's own memory limit (memory_limit in php.ini) was hit. You can increase that limit to fix it. 'Out of memory' means the operating system ran out of real RAM. Raising memory_limit will not help — the RAM is simply not there.

Can adding swap memory fix this error?

It can prevent a hard crash during brief memory spikes. Swap is disk space used as overflow RAM. It is much slower than real RAM, so it is not a long-term solution. For a consistently overloaded server, upgrading to a plan with more RAM is the real fix.

How much RAM does a typical PHP web server need?

A basic PHP site serving modest traffic needs at least 1GB RAM. A WordPress or Magento site with decent traffic needs 2–4GB. Each PHP-FPM worker uses roughly 20–50MB, plus MySQL, the web server, and the OS all need memory. Monitor your usage with tools like htop or New Relic.