Ad Space — Top Banner

Fatal error: Allowed memory size exhausted

PHP Programming Language

Severity: Critical

What Does This Error Mean?

This error means your PHP script used more memory than PHP was configured to allow. PHP killed the script to protect the server. The default memory limit is 128MB, but some scripts — especially those processing large files or arrays — can exceed that easily.

Affected Models

  • PHP 5.x
  • PHP 7.x
  • PHP 8.x
  • All PHP versions

Common Causes

  • Processing a very large file or database result set all at once
  • A loop that keeps adding data to an array without freeing memory
  • Loading an entire image into memory for resizing without releasing it
  • A recursive function that calls itself too many times and builds up data
  • The memory_limit in php.ini is set too low for the task

How to Fix It

  1. Find the memory limit in your php.ini file. Look for the line: memory_limit = 128M. Raise it to 256M or 512M.

    You can also set it at the top of a single script using: ini_set('memory_limit', '256M');

  2. If you are processing a large database result, use pagination or fetch rows one at a time instead of loading all rows at once.

    Fetching 100,000 rows into an array at once can easily exhaust memory. Process in batches of 500 or 1000.

  3. After you are done with a large variable, unset it: unset($bigArray); This tells PHP it can free that memory.

    PHP does not always release memory immediately, but unset() helps significantly on long scripts.

  4. If you are processing images, make sure you destroy the image resource when done: imagedestroy($image);

    Image processing is one of the biggest causes of memory exhaustion in PHP.

  5. Check for infinite loops or deeply recursive functions. Add a counter or depth limit to prevent runaway memory use.

    A function that calls itself with no exit condition will consume memory until PHP kills the script.

When to Call a Professional

If you keep hitting memory limits even after raising them, you likely have a memory leak in your code. A PHP developer can audit your code and find where memory is not being released. This is especially important for long-running scripts or high-traffic applications.

Frequently Asked Questions

Is it safe to just raise the memory limit to fix this?

Raising the limit is a quick fix, but it is not always the right fix. If your script genuinely needs more memory, raising the limit is fine. But if your code has a memory leak, you are just delaying the problem. Fix the root cause when possible.

What is a reasonable memory limit for PHP?

For most websites, 128MB is enough. For WordPress or e-commerce sites, 256MB is common. For scripts processing large files or images, 512MB may be needed. Avoid setting it to -1 (unlimited) on shared or production servers.

How do I check how much memory my script is using?

Use memory_get_usage() to check current memory use. Use memory_get_peak_usage() to see the highest amount used during the script. Add these at key points in your code to find where memory spikes.