Allowed memory size of 33554432 bytes exhausted (tried to allocate 43148176 bytes) in php
Solution 1
If your script is expected to allocate that big amount of memory, then you can increase the memory limit by adding this line to your php file
ini_set('memory_limit', '44M');
where 44M
is the amount you expect to be consumed.
However, most of time this error message means that the script is doing something wrong and increasing the memory limit will just result in the same error message with different numbers.
Therefore, instead of increasing the memory limit you must rewrite the code so it won't allocate that much memory. For example, processing large amounts of data in smaller chunks, unsetting variables that hold large values but not needed anymore, etc.
Solution 2
Here are two simple methods to increase the limit on shared hosting:
If you have access to your PHP.ini file, change the line in PHP.ini If your line shows 32M try 64M:
memory_limit = 64M ; Maximum amount of memory a script may consume (64MB)
If you don't have access to PHP.ini try adding this to an .htaccess file:
php_value memory_limit 64M
Solution 3
Your script is using too much memory. This can often happen in PHP if you have a loop that has run out of control and you are creating objects or adding to arrays on each pass of the loop.
Check for infinite loops.
If that isn't the problem, try and help out PHP by destroying objects that you are finished with by setting them to null. eg. $OldVar = null;
Check the code where the error actually happens as well. Would you expect that line to be allocating a massive amount of memory? If not, try and figure out what has gone wrong...
Solution 4
Doing :
ini_set('memory_limit', '-1');
is never good. If you want to read a very large file, it is a best practise to copy it bit by bit. Try the following code for best practise.
$path = 'path_to_file_.txt';
$file = fopen($path, 'r');
$len = 1024; // 1MB is reasonable for me. You can choose anything though, but do not make it too big
$output = fread( $file, $len );
while (!feof($file)) {
$output .= fread( $file, $len );
}
fclose($file);
echo 'Output is: ' . $output;
Solution 5
I have faced same problem in php7.2 with laravel 5.6. I just increase the amount of variable memory_limit = 128M
in php.ini as my applications demand. It might be 256M/512M/1048M.....Now it works fine.
panidarapu
Updated on January 26, 2022Comments
-
panidarapu over 2 years
This error message is being presented, any suggestions?
Allowed memory size of 33554432 bytes exhausted (tried to allocate 43148176 bytes) in php
-
CLRBTH over 15 yearsYou still should check why the memory is exhausted. Maybe you don't need to read the whole file, maybe read it sequentially.
-
Don Jones over 14 yearsWorked great. I know why memory was being exhausted - I'm using Zend-Feed to consume a rather large-ish Atom feed. I can't control the size of it or do anything other than swallow the whole feed in one pull, so bumping the memory limit for that one operation solved it. Cheers!
-
anonymous coward almost 14 years- @panidarapu and @Don Jones: Depending on the amount of memory, and how this script is used, it could be dangerous to allow the change in memory usage in this way. Don, in your case, you can likely break the feed down into smaller chunks and parse what you need. Glad it works, but be careful.
-
mente about 13 yearsThis suggestion worked for me. Dynamically increasing memory limit in the script is done via function ini_set(): ini_set('memory_limit', '128M');
-
Kristian J. about 13 years@panidarapu: You might not have permissions to increase the memory limit - especially if you're on a shared host.
-
panidarapu about 13 yearsini_set('memory_limit', '128M'); i used same thing but it showing same problem
-
mixdev about 13 yearsIf this doesn't work, add 'php_value memory_limit 50M' in your .htaccess. Replace 50M with your own value.
-
ekerner almost 13 yearsThis is a bad idea that potentially leaves your server open to malicious attacks via large data posts.
-
dmanexe over 12 yearsIf anyone else is running into this problem with Wordpress, try setting PHP's memory limits within wp-config.php. Here's the code:
define('WP_MEMORY_LIMIT', '128M');
-
Ahmad Hajjar over 12 years@panidarapu To use unlimited memory is too much, isn't it?
-
Jeroen K almost 12 yearsI had this error in /wp-includes/js/tinymce/langs/wp-langs.php. Changing memory wasn't possible/didn;t work. I just deleted most part of the file (from $lang= on )This worked (so now the editor is in english, but so what).
-
Sumoanand almost 11 yearsGuys, please don't go for this quick fix. It can hurt you in long run. As a good programmer, you should figure-out the reason behind this memory consumption & increase as required instead of keeping it
UNLIMITED
. -
Kris Selbekk over 10 yearsI had this exact problem - turned out I had inadvertedly created a recursive function - and thus it ran out of memory at any random time during code execution. This had the upside of me now having the world's most memory efficient code, created in the hunt for a memory leak.
-
Ambidex over 10 yearsWow, this massively upvoted answer seems like really bad practice to me. -1!
-
sam yi about 10 yearsI also feel it's dangerous but what's the worst that can happen in the future? Bring down the server? What if it's down currently? Can it make it get any worse? What if you're using a third party app and cannot change the code?
-
Avatar about 10 yearsSolved this by using:
$fh = fopen($folder.'/'.$filename, "rb") or die(); $buffer = 1024*1024; while (!feof($fh)) { print(fread($fh, $buffer)); flush(); } fclose($fh);
-
peter70 almost 10 yearsI had just also this error in my script. It was because I came in an endless loop, which resulted in the Stack Overflow
-
JohnWolf almost 9 yearsCan't believe all these people recommending to set memory_limit to -1... Crazy thing to do on a production server. Thanks for a much cleaner solution.
-
kodeart over 8 yearsWhile at "best practice", it is good to close the file handler after the
while
loop:fclose($file)
-
chestozo over 8 yearsBetter try to use something like
memory_get_usage
to diagnose the cause. In my case it was an infinite loop. And it is not good to have infinite loops in production :) -
wedstrom almost 8 yearsThis is an AWFUL solution. Only use this to get things working enough so that you can identify the high memory usage and REDUCE it. This will KILL your production server when you get another memory bug. Rather than killing the one bad request, it will knock out quite a bit more.
-
Francis Pierot almost 8 yearsSetting unlimited memory is a very bad idea. It hides the memory consumption of the program, which can certainly be lowered. Moreover, if this is running on a server and called several times a second it will hog the server memory or, probably, end dying on some instances. You MUST analyze your program with memory_get_usage() traces at various places, although values given by this function are sometimes disturbing due to the block-allocation scheme of PHP.
-
Kzqai over 7 yearsIt seems pretty likely that this is what does it.
-
Mustafa sabir about 7 yearsWorks for wordpress websites, where access is not available for .htaccess and php.ini files. +1
-
DavidHyogo almost 7 yearsIt doesn't work on PHP 5.6 on Windows. I get a similar exhausted memory error.
-
nwolybug over 6 yearsThis is a great article on this error and why NOT to use UNLIMITED memory on a server. Use -1 on a dev for testing only but not on production. airpair.com/php/fatal-error-allowed-memory-size
-
DIGITALCRIMINAL about 6 years@Pang ini_set('memory_limit', '-1');
-
helvete about 6 years@assetCorp How does this help, provided the file has for example 100MiB and PHP memory limit is still set to 32 MiB. You read it by secure chunks of 1MiB, but then append it into a variable that is going to use all the available memory once the loop reaches 31. iteration. How is it any better? Only outputting the chunks similarly not to require storing them all in one variable would help to solve the problem.
-
Alex Mulchinock about 6 yearsFollowing guidance here: meta.stackoverflow.com/questions/255198/… I've had to downvote your answer, as it doesn't fix the problem you described, only masks it - and potentially causes further issues later down the line.
-
Matthew Barbara over 5 yearsThis is very bad. If you do this, you might consume a considerable amount of server memory and consequences might be critical
-
Desislav Kamenov about 5 yearsFor the sake of others who will chase a rabbit down a hole. Doctrine in Symfony i think has an issue with monolog and when there is PDO exception it will create an infinite loop of exceptions as it will try an exception for the exception thus hiding the real issue (a corrupted db file in my case).
-
Daydah about 5 yearsI agree this is a bad solution because of the potential future risk it creates, but I chose it because it was a quick fix. I was trying to update a Joomla! install to the latest version. Once I was able to do that, I removed the ini_set addition immediately.
-
Mohammed Muzammil almost 5 yearsthis is not a recommended method. it's like you are telling to take as much as memory needed. so run this command "php -d memory_limit=4G bin/magento" in Magento root directory
-
zanderwar over 4 yearsWhen you don't like your webserver, set memory limit to infinite... LOL
-
Grald over 4 yearsawesome init_set but I have a question is this safe to use for a server?
-
Gwyneth Llewelyn over 4 yearsI'd say that changing those limits on a 'core' WordPress file is not really a good idea; you can so very easily add those limits on
wp-config.php
instead, where they will not get overwritten by future WordPress updates. Also, a few security plugins (such as WordFence, for instance) will complain if 'core' WordPress files are changed... -
garakchy over 4 years@GwynethLlewelyn I don't know how to do that, can you elaborate it, please?
-
Gwyneth Llewelyn over 4 yearsOh... just edit
wp-config.php
and add the two lines there (i .e.define( 'WP_MEMORY_LIMIT', '200M' );
anddefine( 'WP_MAX_MEMORY_LIMIT', '256M' );
. Unlike the files on the 'core' WP (namely, everything underwp-includes
), which will be overwritten by WP upgrades,wp-config.php
will not — it's there exactly for the exact purpose of overriding WP constants! -
emmdee over 4 years"bad practice" is situational.
-1
is fine for short-running processes. For example a php builder container that's used for running unit tests or composer installs/etc. Just don't run your production site with it set like that. -
Sayem about 4 yearsI 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 —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. If by increasing the memory limit you have gotten rid of the error and your code now works, you'll need to take measures to decrease that memory usage.
-
neo about 4 yearsIn your .htaccess you can add: PHP 5.x <IfModule mod_php5.c> php_value memory_limit 64M </IfModule> PHP 7.x <IfModule mod_php7.c> php_value memory_limit 64M </IfModule>
-
Elron over 3 years
ini_set('memory_limit', '44M');
worked well in my wordpress case. -
user1994 over 3 yearsi''m on 7.4, still facing.
-
successtar over 3 yearsThe issue was resolved when I upgrade php to 7.1. Can you try php 7.1 or 7.2
-
Hiran Chaudhuri about 3 yearsThis does not really solve the issue, it just is a workaround for a still existing problem.
-
Maksim Dimitrov almost 2 yearsIf in hurry do ini_set('memory_limit', '-1'); and if it is a one time operation, like a seeder, return it then to the previous value