Allowed memory size of X bytes exhausted

115,614

Solution 1

PHP's config can be set in multiple places:

  1. master system php.ini (usually in /etc somewhere)
  2. somewhere in Apache's configuration (httpd.conf or a per-site .conf file, via php_value)
  3. CLI & CGI can have a different php.ini (use the command php -i | grep memory_limit to check the CLI conf)
  4. local .htaccess files (also php_value)
  5. in-script (via ini_set())

In PHPinfo's output, the "Master" value is the compiled-in default value, and the "Local" value is what's actually in effect. It can be either unchanged from the default, or overridden in any of the above locations.

Also note that PHP generally has different .ini files for command-line and webserver-based operation. Checking phpinfo() from the command line will report different values than if you'd run it in a web-based script.

Solution 2

ini_set('memory_limit', '128M'); 

or

php.ini  =>  memory_limit = 128M

or

php_value memory_limit 128M

Solution 3

I had same issue. I found the answer:

ini_set('memory_limit', '-1');

Note: It will take unlimited memory usage of server.

Update: Use this carefully as this might slow down your system if the PHP script starts using an excessive amount of memory, causing a lot of swap space usage. You can use this if you know program will not take much memory and also you don't know how much to set it right now. But you will eventually find it how much memory you require for that program.

You should always memory limit as some value as answered by @şarkı dinle.

ini_set('memory_limit', '512M');

Giving unlimited memory is bad practice, rather we should give some max limit that we can bear and then optimise our code or add some RAMs.

Solution 4

The memory must be configured in several places.
Set memory_limit to 512M:

sudo vi /etc/php5/cgi/php.ini
sudo vi /etc/php5/cli/php.ini
sudo vi /etc/php5/apache2/php.ini Or /etc/php5/fpm/php.ini

Restart service:

sudo service service php5-fpm restart
sudo service service nginx restart

or

sudo service apache2 restart

Finally it should solve the problem of the memory_limit

Solution 5

If you're sure you restarted Apache after configuring php.ini, then you might be looking at the wrong php.ini file

Share:
115,614
Nathan
Author by

Nathan

Updated on July 09, 2022

Comments

  • Nathan
    Nathan almost 2 years

    Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 13965430 bytes)

    PHPInfo shows that I have a memory_limit of 128M, so I'm confused as to why the error says I only have 64M. Is it possible for phpinfo to report incorrectly? Or for PHP to use two separate php.inis?

    The error was being caused by an ini_set call in one of the primary php files that a co-worker of mine added without my knowledge.

  • Nathan
    Nathan over 13 years
    Well, when I type phpinfo in the shell, it says memory_limit is 128, but then on the page it says 64. I'm not sure what's going on anymore, this flu has my head all jacked up.
  • Marc B
    Marc B over 13 years
    PHP also has different .ini files for commandline operation and for webserver operation. CHanging it in one won't affect the other, unless BOTH are using the same .ini file. You need to run phpinfo() in a web-based script instead.
  • Nathan
    Nathan over 13 years
    Hmm, I wasn't entirely aware of that. However, I just checked phpinfo() in a PHP file from the same directory as the other file, and both the Local and Master values are 128M. :(
  • Nathan
    Nathan over 13 years
    Update: I just updated the script and added ini_set before it ran to increase the memory to 256, phpinfo reported that it increased locally, but the script still failed and said it was only 64M.
  • Dr.Molle
    Dr.Molle over 13 years
    increasing memory_limit could be restricted by other settings, for example suhosin-patch. Maybe phpinfo() doesn't determine this and returns an incorrect value.
  • Anand Solanki
    Anand Solanki over 10 years
    Good Answer. All possible options are mentioned.
  • Wyatt Barnett
    Wyatt Barnett almost 10 years
    Works fine until something hits you hard and your server runs out of memory.
  • Nathan
    Nathan almost 9 years
    This isn't completely necessary, depending upon the setup and desired result. For this question in particular, I needed it increased only for one action, not for the entire application.
  • Bikal Basnet
    Bikal Basnet over 7 years
    memory_limit exists for a reason so that your server wont crash or bear any physical damage due overload. Giving it unlimited memory is bad practice, rather we should give some max limit that we can bear and then optimize our code or add some RAMs.
  • Somnath Muluk
    Somnath Muluk over 7 years
    @WyattBarnett: Thanks for pointing out. I was fresher at that time and not taking things seriously. Now I understand what did you mean. (Just missed your comment). Updated answer.
  • Somnath Muluk
    Somnath Muluk over 7 years
    @BikalBasnet: Thanks for the comment on old answer which was misleading. I have updated answer with some part of your comment.
  • Nathan
    Nathan about 5 years
    This answer provides no new information. In fact, I would argue that this answer's strong bias towards Apache makes it irrelevant. Not everyone uses Apache and this question was about PHP, not a web-server.
  • heySushil
    heySushil about 5 years
    I share this knowledge just because sometimes it's hard to changes on your local php.ini file and when deploy on the server again do the same. for this, I'll share this.
  • Nathan
    Nathan about 5 years
    But your answer doesn't address the problem, it addresses a similar problem that was not asked here.