How to Solve PHP Fatal error: Allowed memory size of 8388608 bytes exhausted

If you’re plagued by a PHP fatal flaw related to excessive memory size, we’ve got directions to work around the error until you can debug your code.

One of the most common and frustrating errors encountered by PHP coders is a fatal error related to memory size. Such an error 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 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).

Fixing a Fatal Memory Size Error in PHP

To workaround the “Fatal error: Allowed memory size of XXX bytes exhausted…” error message, simply insert this line of code into your script at the top:


memory_limit in php.ini

Replace XX above with the amount of memory you want to allocate to PHP. In the screenshot above, the configuration will set your memory limit to 16MB. 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:


Fatal error: Allowed memory size of 134217728 exhausted

Same Error Over the Years, but with Larger Numbers

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 increased from 8M to 16M in PHP 5.2.0 and is now 128M for more recent versions of PHP. This explains 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.


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

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

Troubleshoot Your Code So This Workaround Is Unnecessary

Now, remember, this is only a workaround. Really, your PHP script should not be exceeding 8MB, 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. Rudy

    January 5, 2011 at 5:51 am

    This is useful information, although slightly confusing. As the filenames you mention, do not match the images. In addition, I do not have a content.php file on my server.

    Could you advise?

    Thanks, in advance!

    • groovinJackman

      January 5, 2011 at 7:38 am

      I just used content.php as an example.

      Basically, there are three fixes that should be use separately (use just one–not all three):

      1. Add ini_set(“memory_limit”,”16M“); to the beginning of your individual PHP file that causes the error. For example, if index.php is causing the error, add to the top of index.php. This will only apply the memory limit change to that file.
      2. Change the memory_limit= line in your php.ini file. You may not have access to this, depending on your web hosting package. You may need to contact your administrator. This is a site-wide change that affects your entire PHP installation.
      3. Add the memory_limit= line to your .htaccess file (or create a .htaccess file and put it in the folder where you want it to be applied). This may be a better option if you don’t have access to php.ini. This will be a directory-wide change. Say, if you add it to the .htaccess file of it’ll only affect php files in the /section1 subdirectory.

      • Karen

        February 7, 2011 at 1:17 pm

        Thanks for this. I think the reason why It’s not working for me is that I made the changes across php.ini, problematic php file, htaccess in root and htaccess for the blog, – plus made the same changes too in wp-config.php and in wp-settings. My host said my limit is 64mb but i was told i can ask help on how to optmize it or upgrade to VPS whihc im not ready for yet. I’m gonna try to undo the changes or keep the changes to one of these. Hope it works this time. Been on this issue for almost 2 days now, such a show stopper :(

        Fatal error: Out of memory (allocated 66846720) (tried to allocate 35 bytes) in /home/maxlife/public_html/ on line 311

      • Karen

        February 7, 2011 at 3:07 pm

        What exact line do I add in the .htaccess file?

        This is how it looked like when I added the line in my .htaccess file (root public_html):

        suPHP_ConfigPath /home/maxlife

        order allow,deny
        deny from all

        memory_limit = 128M

        It returned an internal server error.

        I removed the line and tried to revise instead, the other .htaccess file within the subdirectory in question. This is how it looked like after I added the line:

        # BEGIN WordPress
        RewriteEngine On
        RewriteBase /
        RewriteRule ^index\.php$ – [L]

        # uploaded files
        RewriteRule ^files/(.+) wp-includes/ms-files.php?file=$1 [L]

        RewriteCond %{REQUEST_FILENAME} -f [OR]
        RewriteCond %{REQUEST_FILENAME} -d
        RewriteRule ^ – [L]
        RewriteRule . index.php [L]
        memory_limit = 128M
        # END WordPress

        This didn’t work either. it returned again an internal server error.

        I also tried recommendations 1 & 2, I got the same fatal error message.

        What other free solutions can I explore please? Really need your help.


        • Karen

          February 7, 2011 at 10:22 pm

          OK, my problem just got solved!!!

          This was what I did:

          ENTER AFTER:
define(‘ABSPATH’, dirname(__FILE__).’/’);
          THE FOLLOWING:

          define(‘WP_MEMORY_LIMIT’, ’64M’);


          memory_limit = 128M; Maximum amount of memory a script may consume (64MB)

          max_execution_time = 45;

          upload_max_filesize = 10M;

          post_max_size = 20M;

          Hope this helps!

          • insideoum

            October 30, 2011 at 6:20 pm

            @Jack: thanks for the article, It’s very useful and Karen’s trick is working as well :)

          • Kadiest Johnson

            October 4, 2013 at 12:00 pm

            Hey Karen, you are a life saver. i was on the same problem for 2 days myself. after research page after research page, i come across yours and it worked. great job.

            -Kadiest Johnson

  2. voxpop

    January 5, 2011 at 3:37 pm

    wish someone would solve this error message 0x80040508
    has to do with installing rockmelt which is based on google chrome which I have installed with no problem.

  3. Marvio Rocha

    October 26, 2011 at 9:27 pm

    Very nice,now my php has been setting

    Thank you!

    • Steve Krause

      October 27, 2011 at 3:51 pm

      @Marvio – your welcome. Thnx for the comment and welcome to groovyPost.

      • Marvio Rocha

        May 29, 2013 at 5:31 pm

        Hi Steve,

        What is your name that plugin reply comment? It is marvelous for community wordpress. Please what’s your plugin?

        • Steve Krause

          May 29, 2013 at 8:49 pm

          Standard wordpress comments.

  4. Irfan

    April 9, 2012 at 4:15 am

    I was facing the problem of script size that have removed by using the first technique mentioned on top of the page.
    These are really very nice and helpful posts.

    • Steve Krause

      April 9, 2012 at 11:53 am

      @Irfan – Excellent. I’m glad we could help and thanks for the feedback!

  5. Chetan

    May 19, 2012 at 7:16 am

    but where is the file located?

  6. Malcolm Finlayson

    October 23, 2012 at 1:35 pm

    Thanks. I hope this solves my problem as it is creeping into my overnight export cron job.

  7. PsLot

    May 20, 2013 at 6:31 am

    Pls I have been encountering this problem for over a week

    I’m trying to set up a Joomla website LOCALLY by using a quickstart Responsive template (LightHouse).

    1st of all, I got the Fatal error: Allowed memory size of 8388608 bytes exhausted, WHICH HAS BEEN SOLVED BY YOUR METHOD.

    Then I’m now getting this error
    Out of memory (allocated 1832910848) (tried to allocate 24576 bytes) in C:\xampp\htdocs\HospitalChapel\templates\lighthouse\index.php on line 40

    I increased the memory limit in php.ini. file but to no avail

    Then I put a .htaccess file in the directory where the problem was generated and nothing happened.

    Pls how can I solve this problem

  8. Andy

    May 29, 2013 at 1:20 pm

    Thanks so much for the tip!

  9. tatis

    April 28, 2014 at 12:53 pm

    It help me a lot, thank you very much.

  10. M

    July 7, 2015 at 9:17 am

    I added to same to the .php but this is not resolving.
    Any more help?

    This actually happens when I activate the Google Fonts wordpress plugin.

  11. Pranav

    July 9, 2019 at 9:11 pm

    I am facing this issue for php script. It says “Allowed memory size of 4294967296 bytes exhausted (tried to allocate 2097160 bytes)”. SHould I need to set the memory to 4GB in my script file? Any alternative for this?

  12. jack

    December 9, 2022 at 8:51 am

    Not all heros wear capes!

Leave a Reply

Your email address will not be published. Required fields are marked *


To Top