Fatal error: Allowed memory size of 8388608 bytes exhausted.
sometime PHP script may returns the above error. Normally this error is generated when your script exchausted and used up the default memory requirement of 8 MB memory allocation.
* Default memory limit is set in php.ini file ( php configuration file.)
* memory_limit = 8M;
How can we manage with the memory problems comes in some php scripts?
you should check following things when such kind of memory errors occurs in your script.
1. check the default memory_limit variable in your php.ini file.
memory_limit = 8M;
you can change this memory limit and again check for the error.
a. Edit in php.ini
memory_limit = 12M;
restart the apache service.
b. code Level
php_value memory_limit 12M
2. If the error is not resolved after above step, then you should start debugging your code in order to find out the reason of memory limit exceed.
You have to first find out at what position in your code the memory limit is exceeding. you can use the php's built in function for this purpose.
memory_get_usage — This function returns the amount of memory allocated to PHP.
Syntax : int memory_get_usage ( true / false );
It returns the amount of memory, in bytes, that's currently being allocated to your PHP script.
Set the option to this function to TRUE to get the real size of memory allocated from system. If not set or FALSE only the memory used by emalloc() is returned.
How to use this function
1. Write this code ( echo memory_get_usage( true ); ) repeteadly after some no of lines.
2. Run it in browser
3. Compare the counts given by each echo and you can check which block of your code needs extra memory.
4. Once you identify the block of code which needs extra memory then you can start deallocating the unused memory spaces allocated by some unused variables and some infinite loops.
5. use following wherever necessary.
b. mysql_free_result($result_set); — Free result memory
c. Look for include("file.php") in loops, by mistake.
Just try to free the memory everywhere if the variable / array is no longer used.
6. Best of luck.
bool mysql_free_result ( resource $result )
It frees all the memory associated with the result identifier result .