Allowed memory size of X bytes exhausted

Allowed memory size of X bytes exhausted

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

Stefan Bogdanescu

Founder & Senior Architect

Allowed memory size of X bytes exhausted

When you encounter a Fatal error: Allowed memory size of 67108864 bytes exhausted, the immediate reaction is often confusion, especially when your system configuration appears to suggest otherwise. In the scenario described, the error message indicates a hard limit of exactly 64 MB (67108864 bytes), yet phpinfo() reports a memory_limit of 128 MB. This discrepancy usually stems from a misunderstanding of how PHP's configuration hierarchy resolves itself at runtime, rather than a bug in the diagnostic tool.

PHP does not simply read a single global configuration file to determine its limits. Instead, it follows a strict precedence order when resolving settings like memory_limit. The engine checks for overrides in that specific order: command-line arguments, .htaccess files (if enabled), environment variables, and finally the loaded php.ini files.

The source material highlights a critical detail: an ini_set() call was added to one of the primary application files. This is a common anti-pattern that often leads to exactly this kind of confusion. When you use ini_set('memory_limit', '128M') inside your code, you are instructing PHP to change the limit for the current process. However, the error message reflects the actual memory ceiling enforced by the runtime environment after all configuration layers have been applied.

If your application runs under Apache or Nginx with php.ini loaded via .htaccess, and that file sets memory_limit = 128M, but a script later executes ini_set('memory_limit', '64M'), the limit drops to 64 MB. Conversely, if the underlying php.ini or environment variable is set to 64 MB, an attempt to raise it via ini_set() will fail silently or be ignored depending on whether the new value is greater than the current one, but usually, ini_set only works if the setting is allowed to change. More importantly, if your server environment (e.g., a specific PHP-FPM pool definition) sets a hard limit of 64 MB via an external config file, that override takes precedence over any php.ini settings you might see in your web root or the main configuration directory.

Consider this common pitfall:

// Attempting to raise memory usage dynamically
ini_set('memory_limit', '256M');

Even if this line executes without error, it will only take effect if the underlying system allows the increase and if memory_limit is not set to a value that restricts dynamic changes. If your server environment (like a specific Docker container or Nginx/Apache pool) has restricted memory to 64 MB at the OS or process-manager level, PHP will never exceed that regardless of what you set in code or php.ini.

The error message explicitly states "tried to allocate 13965430 bytes". This specific allocation request failed because the running process hit its ceiling. The fact that phpinfo() shows 128 MB suggests that the diagnostic function was called before the dynamic ini_set call took effect, or it is reading from a different configuration file than the one currently active for the failing script.

To resolve this, you must inspect the effective configuration at runtime. Running php -i | grep memory_limit directly on the server (outside of your application context) often reveals the true limit enforced by the SPM or container. If you are using a framework like Laravel, ensure you are not relying solely on config/app.php for memory limits, as they can be overridden by the PHP-FPM pool configuration files located in /etc/php/.../fpm/pool.d/.

The most robust fix is to identify which configuration file or environment variable is enforcing the 64 MB limit and adjust it there. If you control the server, increasing the memory_limit in the main php.ini or the specific pool definition is safer than relying on ini_set() within application code, which can be unpredictable across different hosting environments.

For further guidance on optimizing memory usage in large-scale PHP applications or containerized environments, resources like https://laravelcompany.com offer practical advice on scaling your infrastructure to handle heavy processing tasks without running into resource exhaustion errors.

Our network