How to fix PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 12038704 bytes)

Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 12038704 bytes)
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 12038704 bytes)

Introduction

The Allowed memory size of X bytes exhausted fatal error is a common issue developers and website administrators encounter. 
It indicates that a PHP script has attempted to allocate more memory than the limit set by your PHP configuration. The number of bytes (like 134217728 bytes, which equals 134 megabytes (MB)) will vary, but the cause is always the same: a script ran out of allocated memory (PHP memory_limit).
 

This typically happens during resource-intensive operations or misconfiguration, listed cases:

  • Processing large image uploads or manipulations.
  • PHP setup misconfiguration.
  • Running complex reports or database queries that return massive datasets.
  • Handling large arrays or deeply nested structures.
  • Using inefficient code that leads to a memory leak or excessive object creation.

This tutorial will guide you through the most effective methods for increasing the PHP memory limit to resolve this error.

The error logged in an error log:

[11-Jun-2025 03:00:55 America/New_York] PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 12038704 bytes) in /var/www/html/index.php on line 346

 

Prerequisites

To follow this tutorial, you will need:
Ubuntu 22.04, 20.04, 18.04 or older server with a sudo non-root user.
Installed Apache2, LAMP Stack
Installed PHP, PHP-FPM
 

 

Solution: Methods to Increase the PHP Memory Limit

You can increase the memory limit using several methods, depending on your level of server access and preferred approach. 

 

Method 1: Modifying the php.ini file (Apache2 prefork)
; Maximum amount of memory a script may consume
; http://php.net/memory-limit
memory_limit = 512M

The php.ini file is the default configuration file for running applications that require PHP. This is the most robust and preferred method if you have access to it (usually on a VPS or dedicated server, or sometimes in shared hosting control panels).

If installed Apache2 (prefork) and php mod

Example location:
/etc/php/8.0/apache2/php.ini

Warning:
Apache2 service reboot is needed after the changes are made. 

 

Method 2: Modifying the php.ini file (Apache2 PHP-FPM)
; Maximum amount of memory a script may consume
; http://php.net/memory-limit
memory_limit = 512M

Changes to php.ini file default configuration to increase PHP memory_limit to 512 Mb

If installed Apache2 (event) and PHP-FPM

Example location:
/etc/php/8.0/fpm/php.ini

Warning:
PHP-FPM service reboot is needed after the changes are made. 

Method 3: increase the memory_limit using an .htaccess file
php_value memory_limit 512M

Location: .htaccess
(the file is usually located in the root directory of the site)

If you are on a shared hosting environment and do not have access to the main php.ini file, you can often override certain settings using the .htaccess file in your website's root directory (or the directory of the script causing the error). This method works only if PHP is running as an Apache module and the hosting provider allows this override.

Summary

The "Allowed memory size exhausted" error is a clear signal that your script needs more resources than currently configured. The most effective and recommended long-term solution is to increase the memory_limit directive in the php.ini file to a reasonable value like 256MB or 512MB and then restart your web server.

After adjusting the limit, monitor your application. If the error persists, consider that your code may have a genuine memory leak or inefficiency that requires optimization rather than just more memory.