PHP Fatal Error: Allowed Memory Size — Breaking Myths and Configuring

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why Does Your PHP Script "Explode"?

\n\n

Have you ever seen a white screen with the chilling message "Fatal Error: Allowed Memory Size of 134217728 Bytes Exhausted"? If so, you are not alone. This question on Stack Overflow has gathered thousands of views, and every second PHP developer has encountered it at least once in their life.

\n\n

The number 134217728 bytes is exactly 128 megabytes (the standard limit for many hosting providers). When your script tries to use more, PHP says "stop" and throws a fatal error. But don't panic! In this article, we'll break down why this happens, how to fix it, and — most importantly — how to optimize performance so the memory error no longer bothers you.

\n\n

This article is written for the educational website PythonLib, but all the code and examples are in PHP. Let's go!

\n\n

What is memory_limit and Where Does "134217728" Come From?

\n\n

In PHP, there is a directive memory_limit that sets the maximum amount of memory available to a single script. By default, in many configurations (especially on shared hosting), it is set to 128M (or 134217728 bytes).

\n\n

You can check the current limit like this:

\n\n
<?php\necho ini_get('memory_limit'); // Will output, for example, "128M"\n?>
\n\n

When your script exceeds this limit, PHP immediately terminates execution with a fatal error. The reasons can vary: from non-optimal code to loading giant files into memory.

\n\n

Typical Scenarios That Cause the Error

\n\n
    \n
  • Working with large arrays — for example, reading an entire CSV file into memory.
  • \n
  • Loops with leaks — forgetting to free variables or using global arrays.
  • \n
  • Image processing libraries — GD or Imagick when working with high-resolution photos.
  • \n
  • Recursive functions — without a base condition, they go infinite and consume the stack.
  • \n
  • ORM and caching — some ORMs (e.g., Doctrine) may load an entire table into memory.
  • \n
\n\n

How to Fix the Error: 3 Proven Methods

\n\n

1. Temporarily Increase the Limit via ini_set()

\n\n

The fastest way is to increase the limit directly in the script:

\n\n
<?php\nini_set('memory_limit', '256M');\n// Now the script can use up to 256 MB\n?>
\n\n

Important: This will only work if your hosting allows overriding the directive. On some hosting providers, ini_set() for memory_limit is blocked.

\n\n

2. Modify php.ini or .htaccess

\n\n

If you have server access, edit php.ini:

\n\n
memory_limit = 256M
\n\n

Or via .htaccess (on Apache):

\n\n
php_value memory_limit 256M
\n\n

After making changes, restart the web server or PHP-FPM.

\n\n

3. Code Optimization — The Best Way

\n\n

Increasing the limit is a crutch. The real solution is to write efficient code. Let's look at examples.

\n\n

Real Example: Processing a Large CSV File

\n\n

Suppose you have a CSV file with 500,000 rows. Naive code might look like this:

\n\n
<?php\n$rows = file('data.csv'); // Loads the ENTIRE file into memory!\nforeach ($rows as $row) {\n    // processing\n}\n?>
\n\n

With a file size of 200 MB, the script will crash with a memory error.

\n\n

Optimization: Reading Line by Line

\n\n
<?php\n$handle = fopen('data.csv', 'r');\nif ($handle) {\n    while (($row = fgetcsv($handle)) !== false) {\n        // process one row at a time\n        // memory usage barely increases\n    }\n    fclose($handle);\n}\n?>
\n\n

Now the script uses only a few megabytes, regardless of the file size.

\n\n<

Blogs

Book Recommendations