Saturday, April 28, 2012

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 65536 bytes) in ....

The error above was found on a LAMP (Centos 6, Apache, MySQL and PHP 5) environment.

Where is the problem?

Facts:
134,217,728 bytes is equivalent to 128MB. These numbers indicate that a limit of some memory has been reached.

2 power of 16 = 65536
2 power of 27 = 134217728


There some sites that suggest to increase the memory limit of PHP with memory_limit directive in php.ini file. But this will only prolong the problem to another day. How are you going to keep increasing the memory limit?

Possible approach:

  1. Some where along the setup, the application is eating up memory through an infinite loop. Review location of the application file (model) that is causing the error.
  2. It states "Fatal error" and not "Php Fatal error", indicating Apache web server is sending the error instead of PHP. Check if web server was restarted after the memory_limit was increased in the /etc/php.ini file.
  3. Database configuration needs to be edited for large selects or join processing. (High performance Innodb)


Troubleshooting (approach 1):
  1. Use phpinfo() to compare the memory_limit with what was set in php.ini file. Check Loaded Configuration File, did it say which php.ini file was loaded? This file can be placed in many places.
  2. In PHP, apply the memory_get_usage() to display memory in used.
  3. For database related calls, use the function mysql_free_result() to release memory used by variables.
  4. If the memory grows large, it may be possible to remove the object from the memory via the unset() function.
Troubleshooting (approach 3):
  1. Create a large database and execute various calls via the web browser. Sample large database can be found at Wikipedia.

No comments:

Blog Archive