How do i fix php fatal error allowed memory size 8388608 bytes exhausted?

One of the most common and frustrating errors encountered by PHP coders reads: “Fatal error: Allowed memory size of 8388608 bytes exhausted…” followed by something like “[tried to allocate XXXX bytes] in /home/www/file.module on line 12.” This fatal PHP error crops up because, by default, PHP has a memory usage limit of 8 MB for any given script. This is a good thing, actually, because you don’t want a rogue PHP script to bring down your server by hogging all the memory. But occasionally, you’ll have a PHP script that normally exceeds the 8 MB limit [say, for importing or uploading]. To workaround the “Fatal error: Allowed memory size of 8388608 bytes exhausted…” error message, simply insert this line of code into your script at the top:

ini_set[“memory_limit”,”16M“];

This will set your memory limit to 16 MB, rather than 8 MB. You can, and should, fiddle with this number so that it is as low as possible without repeating that error message. This will only alter the memory limit for that particular PHP file.

Alternately, you can alter your php.ini file to up the memory limit. This will affect all scripts on your server. Simply open php.ini and find the line that reads “memory_limit” and alter it:

memory_limit=16M

I’ve noticed in my own PHP.ini file that my default is much higher at 128M. So, if I were to ever receive this error message it would read: “Fatal error: Allowed memory size of 134217728 bytes exhausted…” and obviously be a much bigger problem. But it has the same workaround as “Fatal error: Allowed memory size of 8388608 bytes exhausted…” or “Fatal error: Allowed memory size of 16777216 bytes exhausted…” or whatever. Apparently, the  memory_limit default was upped from 8M to 16M in PHP 5.2.0 and is now 128M for PHP 5.3.0, which would explain why you may not get this error message at all.

You can also disable the memory limit by setting memory_limit to –1 in PHP.ini.

memory_limit=-1

This isn’t usually a good idea, though, for obvious reasons.

Note: You can also use the memory_limit line in your .htaccess page.

Now, remember, this is only a workaround. Really, your PHP script should not be exceeding 8 MB, unless your uploading files or doing something else that’s obviously taking up a lot of memory usage. What you should really be doing is trying to figure out why your script is using so much memory and attempt to fix it. One way to figure out how much memory your PHP script is using is to use the memory_get_usage[] PHP function. Simply echo it at any point in your script to find out where your memory usage is spiking:

echo memory_get_usage[];

If you’re getting this error message in Drupal or Joomla, the likely culprit is a new module or package. For example, in Drupal, the admin/modules page loads every module in your Drupal installation, which can get hairy if a custom module is buggy, corrupt or hacked. Try disabling modules one by one to identify which is bringing the party down. Also, some hosting providers will ignore your attempts to modify the memory limits for your PHP code so you might need to contact your hosting support to assist you. I’ve found sometimes I need to edit the php.ini file and sometimes put the code into the .htaccess file. Either way, hopefully, this info here is enough to get you on your way!

1 Introduction: why does Fatal Error happen?

Out-of-memory errors are one of the most common and hard-to-fix problems that PHP developers run into — especially with applications that process large amounts of data — thanks to PHP's relatively conservative default memory settings. In fact, there are more than 1,300 questions related to PHP memory errors on Stack Overflow alone.

98% of the time this error comes from loading more into memory than what you set up PHP to handle in one process. There are other causes, but these are much less common — very rarely it can be a memory leak if you're on PHP 5.3 and above.

If you aren't sure what your PHP memory limit is set to, it's helpfully included in the error message. The size is reported in bytes, though, so we've done some conversions for you:

  • PHP: Fatal Error: Allowed Memory Size of 8388608 Bytes Exhausted - 8 MB
  • PHP: Fatal Error: Allowed Memory Size of 16777216 Bytes Exhausted - 16 MB
  • PHP: Fatal Error: Allowed Memory Size of 33554432 Bytes Exhausted - 32 MB
  • PHP: Fatal Error: Allowed Memory Size of 67108864 Bytes Exhausted - 64 MB
  • PHP: Fatal Error: Allowed Memory Size of 134217728 Bytes Exhausted - 128 MB
  • PHP: Fatal Error: Allowed Memory Size of 268435456 Bytes Exhausted - 256 MB
  • PHP: Fatal Error: Allowed Memory Size of 536870912 Bytes Exhausted - 512 MB
  • PHP: Fatal Error: Allowed Memory Size of 1073741824 Bytes Exhausted - 1 GB

2 What do I have to do to resolve it?

Your first course of action is to increase your memory limit. Note, this is a temporary debugging producedure. The goal is to increase the memory to a point where we have the application working again for the purpose of then reducing the memory usage. Once you decrease the memory usage you can lower the memory limit it to a value that's more suitable. Your plan should be to use as little memory as you could practically use where the application works and functions correctly in a production server based on the workload by your users [humans or programmatic]. I usually recommend setting the memory limit to something high, like 1GB, assuming you have at least 150% of that free in RAM.

Also, never do these tests on a production server unless you're sure you have plenty of RAM and you fully understand how web server processes consume memory. You could easily bring a server to its knees if there are many concurrent processes running, each using a high amount of memory. I would never, ever recommend setting the memory limit to -1 [unlimited] in a production environment. That's a recipe for disaster. Don't make that newbie mistake.

So how do you do this? Simple — increase the memory limit programmatically early on in your code, before your process runs out of memory. If you do it this way, you can give PHP extra memory only when that piece of code gets called rather than increasing the memory limit for all PHP processes.

Chủ Đề