PHP out of memory error even though memory_limit not reached

PHP out of memory error even though memory_limit not reached

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

PHP out of memory error even though memory_limit not reached

You are running a script that crashes consistently at 117 MB, yet ini_get('memory_limit') confirms your php.ini allows 312 MB. You have verified Apache has restarted, logging is active but silent in the Apache logs, and Suhosin is absent. The discrepancy between the configured limit and the hard crash point suggests the issue isn't a simple configuration mismatch. It points toward how PHP manages its internal memory structures or an external process intercepting the request before it reaches the application logic you expect to see.

When PHP reports an "Out of Memory" error, the first instinct is to increase memory_limit. However, that directive only sets the ceiling for the script's heap allocation. If a script crashes at a specific threshold significantly lower than that ceiling, the failure usually stems from one of three areas: fragmentation within the Zend Engine, external processes consuming resources, or a legacy extension enforcing its own hard cap.

Internal Fragmentation and Allocation Patterns

PHP does not allocate memory in a linear fashion. The Zend Engine uses a segregated free list for small allocations and a buddy system for larger blocks. If your script performs thousands of short-lived allocations and deallocations (e.g., creating and destroying arrays or objects in a tight loop), the internal heap can become fragmented. Even if 300 MB is available, there might be no single contiguous block large enough to satisfy a sudden request for memory, causing an immediate crash despite remaining headroom.

To diagnose this, you need to inspect the actual memory usage profile rather than just the peak value. Using memory_get_peak_usage(true) provides a clearer picture of the total footprint including overhead. If the script consistently hits 117 MB and then dies, check if there is a massive allocation occurring right at that boundary. Sometimes, an array expansion or object instantiation triggers a reallocation that fails because the internal fragmentation prevents the move, even though the limit allows it.

External Processes and Shared Memory

A common pitfall in shared hosting or VPS environments running CentOS is assuming memory_limit controls the entire process's RAM consumption. While PHP limits its own heap, other system components can consume memory independently.

Consider the MySQL connection pool. If your script maintains a large number of persistent connections or fetches massive result sets without processing them row by row, the database server itself might be fragmenting its buffer pool or exhausting OS-level limits before PHP even hits its internal limit. Since you are running MySQL 5.1.66-cll, ensure that innodb_buffer_pool_size and other buffers aren't causing the system to swap or OOM-kill the Apache process. If the kernel kills the process due to system-wide memory pressure, PHP might report a generic memory error rather than a specific Fatal Error: Allowed memory size exhausted.

Additionally, check for other daemons running under the same user context (e.g., memcached, redis, or custom cron jobs). If they are competing for RAM, the OS might be swapping heavily, leading to performance degradation that manifests as premature script termination. Use top or vmstat while reproducing the error to see if the swap usage spikes immediately before the crash.

Legacy Extensions and Hard Caps

You mentioned Suhosin is not installed, but PHP 5.3.18 has several other legacy extensions that might enforce stricter limits depending on their build configuration. While less common in vanilla installations, some security modules or specific opcode cache implementations (like APC) can have their own hard limits on memory usage per segment.

If you are using an opcode cache like APC, verify its apc.shm_size and apc.max_memory_per_segment. If a single script requires more than the configured segment size, APC might fail to allocate the necessary space regardless of PHP's memory_limit. This often results in a hard crash at a specific memory threshold that aligns with internal segment sizes rather than the global limit.

Debugging Strategy

To pinpoint the exact cause without public access to the script:

  1. Enable Xdebug Profiling: Enable Xdebug and configure it to generate a profiling report on fatal errors. This can show exactly where the memory spikes occur, helping identify if it's a specific data structure causing fragmentation.
  2. Check phpinfo() for Extensions: Run phpinfo() (via a hidden page or CLI) to list all loaded extensions. Look for any that might have hardcoded limits or known issues with PHP 5.3.18 on CentOS.
  3. Monitor System Resources: Use free -m and top in real-time during execution to see if the system is swapping or if another process is hogging RAM.
  4. Review Apache Error Log Directly: Ensure you are checking the specific error log file path (often /var/log/httpd/error_log or /var/log/apache2/error.log) without filtering, as grep might miss entries if they contain special characters or are written to a different subsystem.

If the issue persists after ruling out fragmentation and external processes, consider upgrading PHP. PHP 5.3.18 is end-of-life and lacks many memory management improvements found in newer versions. Modern PHP versions have better garbage collection heuristics and reduced reliance on fixed-size free lists, which often resolves mysterious hard crashes caused by internal fragmentation.

Our network