Allowed memory size of 268435456 bytes exhausted

Allowed memory size of 268435456 bytes exhausted

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

Stefan Bogdanescu

Founder & Senior Architect

Allowed memory size of 268435456 bytes exhausted

When you encounter the error Allowed memory size of 268435456 bytes exhausted, it is tempting to assume your php.ini configuration is incorrect or that Apache has failed to pick up the new settings. The number 268435456 converts exactly to 256 MB, which was the default memory limit in older PHP versions (pre-5.3) and remains a common default in many distributions today.

In this scenario, you have edited php.ini to set memory_limit = 2048M (2 GB), restarted Apache, and verified the value via phpinfo(). However, the script still dies at 256 MB. This discrepancy usually points to one of two issues: a failure to restart the web server correctly or, more likely in modern environments, the execution environment bypassing the standard php.ini entirely.

The Apache Restart Fallacy

The most common mistake occurs after modifying configuration files. In an Apache installation on Ubuntu, simply running systemctl restart apache2 (or service apache2 restart) is not always sufficient if the server was compiled with specific flags or if the daemon runs under a different user context that doesn't reload the config properly.

To ensure the new settings take effect, you should verify which configuration file Apache is actually using for the relevant virtual host. Run the following command:

grep -R "memory_limit" /etc/php/5*/apache2/conf.d/

If the line exists in conf.d, ensure it wasn't commented out or overridden by a lower-priority file included later in the stack. More importantly, check the process status. If Apache was running as a daemon that didn't fully restart or if you are using a specific module setup (like PHP-FPM), the standard php.ini might not be the active configuration at all.

The Real Culprit: CLI vs. Web Environments

If you are developing locally on Ubuntu, your script is likely being invoked via command-line interface (CLI) or through a terminal session, rather than directly by the Apache web server. This distinction is critical because PHP manages separate configurations for different execution contexts.

When you run phpinfo() in a browser, it reflects the configuration loaded by the Apache module (mod_php). However, when you execute a script from the terminal (e.g., php import.php), it loads a completely different configuration file: php-cli.ini.

By default, most Linux distributions do not include memory limit settings in the CLI configuration. This means that even if you have set memory_limit = 2048M in the Apache config and verified it via phpinfo(), the CLI version of PHP will still fall back to its compiled-in default or a system-wide default, which is often 128 MB or 256 MB.

To fix this, you must edit the CLI-specific configuration file. On Ubuntu, this is typically located at /etc/php/5*/cli/php.ini. You can verify the active CLI config with:

php -i | grep memory_limit

Or by running:

php -r "echo ini_get('memory_limit');"

If you see 256M in the output, your Apache settings are irrelevant to this specific execution. You must explicitly set the limit in the CLI config file:

; /etc/php/5*/cli/php.ini
memory_limit = 2048M

After editing this file, you do not need to restart Apache, as the CLI PHP binary reads its configuration on startup. Simply run your script again.

Handling Large Imports Gracefully

If you are importing an 87 MB database into a script that processes it row-by-row or loads large chunks into memory, hitting the limit is inevitable regardless of the setting. Even with 2 GB available, inefficient code can trigger garbage collection or cause swapping, leading to performance degradation before the actual limit is hit.

If you are using a framework like Laravel, ensure you are testing within the same environment. A common pitfall is running artisan commands or scripts via php artisan which might inherit CLI settings but be confused by web server configurations if not properly isolated. For robust data imports in PHP-based applications, consider processing data in smaller batches and flushing memory manually using gc_collect_cycles() between chunks to prevent accidental accumulation of objects.

Conclusion

The persistence of the 256 MB error despite changing settings in php.ini is almost certainly due to editing the wrong configuration file for the execution context. By targeting the CLI-specific php.ini, you align the runtime environment with your intended limits, resolving the exhaustion error without needing to modify the web server configuration or restart services unnecessarily.

Our network