Posted by: phillipnb | August 9, 2010

Fatal error: Allowed memory size ……….



Fatal error: Allowed memory size of 99887766 bytes exhausted (tried to allocate 40961 bytes).......

How many times have you seen this error which reads “PHP Fatal error: Allowed memory size exhausted”?. Correct answer to this question will be “Many Times”. Well, you are right. Gone are the days when an application written using PHP was hardly a 1000 KB in size and on top of that the application never used more 512 KB of RAM. Today, PHP is used almost everywhere with lots of plugins and add-ons driving up the use of memory to more than 1 GB. The real translation of this error in terms of a layman is “something is using up all the available RAM and finally when the demand exceeds the availability, the program or
application crashes”.Remember that not all users will be getting this error for the same reason.

Enough of this sermon, let us now talk about a few ways of getting rid of this error. If you are a user of a PHP application then here are a few things which you may want to consider to fix this error:

  • If you have access to a file called php.ini, then try changing the line which says, memory_limit = XM (‘X’ is an existing value and it could be 2 or 4 or 10 etc) to memory_limit = 12M or memory_limit = 16M. After changing the memory limit, save this php.ini file, close your application, restart your Apache server (what ever web server you are using) and now start your PHP application again. Most likely the error should be gone by now. If the memory limit is for a particular PHP file then try changing it by adding the line ‘ini_set(“memory_limit”,”12M”);’ to the top of that PHP file
  • If the error could not be fixed from the above step, try different values for the memory limit till you reach a value which should fix this error
  • If you could not still fix the error using the above two steps then it is better that you contact your System Adminstrator or Web Hosting Company and see if a recent upgrade on the server is the culprit
  • If you are using Drupal then you need to try changing the memory limit variable in php.ini file of Drupal’s root, in addition to settings.php and .htaccess files of Drupal
  • If you are using a lots of plugins or other supplementary files, try disabling a few plugins to identify which plugin is causing this error and then try fixing this error

If you are a PHP developer, first check if the particular PHP version that you are using is the source of the problem. If not, and if more than one user is getting this error then it is hundred percent sure that there is a bug in the PHP code. It is most likely that you are using too many arrays or making too many database calls with lots of records getting fetched in each call or it could be a file that you are reading or it could also be a case of an infinite loop. What ever be the reason, you need to rewrite your code so that users do not run out of memory. One way to find the exact spot in your code which is triggering this memory exhaustion will be to use a good PHP code debugger. Another crude way will be to comment the entire code and run one function at a time to trap the error. What ever method you use to find the source of the error, it is necessary to rewrite the code in such a way there are no mammoth use of arrays or any use of circular references or use of too many global variables etc.

Till Next Time
Happy PHPing

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: