Allowed memory size of X bytes exhausted
Asked Answered
P

8

42

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.

Portuguese answered 4/11, 2010 at 12:23 Comment(6)
Needs way more context. Where are you running this?Bolte
It's a script on a website I'm hosting. It's processing a bunch of location data and generating a KML. However, for one particular set of data, it's giving me this error.Portuguese
Are you on a shared server? Could be the owners have disabled memory_limit changes.Drawing
I am on a shared server, but I've spoken with the sysadmin and he hasn't been very helpful. He only told me to increase memory_limit in php.ini. I've also tried ini_set before the script executes, to no avail.Portuguese
Possible duplicate of Allowed memory size of 33554432 bytes exhausted (tried to allocate 43148176 bytes) in phpBrakesman
What really strikes me is the allowed bytes number is bigger than the allocated bytes.Promissory
D
39

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.

Drawing answered 4/11, 2010 at 12:31 Comment(4)
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. :(Portuguese
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.Portuguese
increasing memory_limit could be restricted by other settings, for example suhosin-patch. Maybe phpinfo() doesn't determine this and returns an incorrect value.Cellini
Good Answer. All possible options are mentioned.Pash
S
27
ini_set('memory_limit', '128M'); 

or

php.ini  =>  memory_limit = 128M

or

php_value memory_limit 128M
Supposal answered 26/3, 2011 at 23:14 Comment(1)
Note that the question asks: '...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?'. I don't think this answers those questions.Whitebeam
A
19

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.

Androsphinx answered 14/4, 2012 at 7:21 Comment(4)
Works fine until something hits you hard and your server runs out of memory.Zandrazandt
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.Thiamine
@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.Androsphinx
@BikalBasnet: Thanks for the comment on old answer which was misleading. I have updated answer with some part of your comment.Androsphinx
D
6

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

Desirous answered 8/5, 2015 at 7:21 Comment(1)
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.Portuguese
E
3

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

Elenaelenchus answered 4/11, 2010 at 12:27 Comment(2)
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.Portuguese
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.Drawing
B
2
1 check current limit:
(in my os)php -i | grep limit  => memory_limit => 256M => 256M

2  locate php.ini 
php --ini =>   
Configuration File (php.ini) Path: /etc
Loaded Configuration File:         /etc/php.ini
Scan for additional .ini files in: /etc/php.d
Additional .ini files parsed:      /etc/php.d/curl.ini 
...

3 change memory_limit in php.ini
vi /etc/php.ini
memory_limit = 512M

4 restart nginx and (php-fpm if being used)
service php-fpm restart
service nginx restart
Bushore answered 22/11, 2017 at 3:56 Comment(0)
G
1

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. Here are a few things you could do to decrease it:

If you're reading files, read them line-by-line instead of reading in the complete file into memory. Look at fgets and SplFileObject::fgets. Upgrade to a new version of PHP if you're using PHP 5.3. PHP 5.4 and 5.5 use much less memory.

Avoid loading large datasets into in an array. Instead, go for processing smaller subsets of the larger dataset and, if necessary, persist your data into a database to relieve memory use.

Try the latest version or minor version of a third-party library (1.9.3 vs. your 1.8.2, for instance) and use whichever is more stable. Sometimes newer versions of libraries are written more efficiently.

If you have an uncommon or unstable PHP extension, try upgrading it. It might have a memory leak.

If you're dealing with large files and you simply can't read it line-by-line, try breaking the file into many smaller files and process those individually. Disable PHP extensions that you don't need.

In the problem area, unset variables which contain large amounts of data and aren't required later in the code.

FROM: https://www.airpair.com/php/fatal-error-allowed-memory-size

Giacinta answered 17/12, 2015 at 15:41 Comment(0)
T
0

This problem is happend because of php.ini defined limit was exided but have lot's of solution for this but simple one is to find your local servers folder and on that find the php folder and in that folder have php.ini file which have all declaration of these type setups. You just need to find one and change that value. But in this situation have one big problem once you change in your localhost file but what about server when you want to put your site on server it again produce same problem and you again follow the same for server. But you also know about .htaccess file this is one of the best and single place to do a lot's of things without changing core files. Like you change www routing, removing .php or other extentions from url or add one. Same like that you also difine default values of php.ini here like this - First you need to open the .htaccess file from your root directory or if you don't have this file so create one on root directory of your site and paste this code on it -

php_value upload_max_filesize 1000M
php_value post_max_size 99500M
php_value memory_limit 500M
php_value max_execution_time 300

after changes if you want to check the changes just run the php code

<?php phpinfo(); ?> 

it will show you the php cofigrations all details. So you find your changes.

Note: for defining unlimited just add -1 like php_value memory_limit -1 It's not good and most of the time slow down your server. But if you like to be limit less then this one option is also fit for you. If after refresh your page changes will not reflect you must restart your local server once for changes.

Good Luck. Hope it will help. Want to download the .htaccess file click this.

Ticktack answered 15/3, 2019 at 11:27 Comment(3)
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.Portuguese
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.Ticktack
But your answer doesn't address the problem, it addresses a similar problem that was not asked here.Portuguese

© 2022 - 2024 — McMap. All rights reserved.