08-04-2015, 10:15 PM
I was browsing and noticed this error message on BBHF today, I thought I should share a solution to it to help others currently facing this problem.
Many website owners runs into this error and sometimes it seems as if it's fixed but just to find out that it is not permanently fixed.
If you are getting an error such as:
The reason is that the application/script running at the moment this error occurred does not have enough memory to complete the given task, hence reporting a fatal error message.
Solutions:
There are many quick fixes, but I preferred a permanent solution.
Solution #1.
If you have access to your php.ini file, the simple solution is to location the memory_limit = in your php.ini file and increase the limit.
e.g change :
Solutioin #2 if you do not have access to system php.ini file (because you have a shared hosting account )
then you may be able to create a php.ini in your home directory and add the following to it:
NOTE that some coder will inform you to set the memory limit to '-1' which means UNLIMITED.
In general this is a bad practice and should not be used on any production server.
Solution #3.
If your hosting allow you to modify .htaccess in the root folder of your website.
add the following to the top your .htaccess
( this may not work if you are on a shared hosting, if you get error 500 after saving, just go back and edit the same file .htaccess and remove the comment the line with php_value like this #php_value memory_limit 256M )
Save it.
Permanent solution:
Since the more visitors on a website uses more resources, (memory etc) dynamically. Thus, a quick fix of increasing the memory will not be a good idea for a website that receive constant traffic.
I quickly wrote this piece of code to automate the process.
In my code, I set the memory not to drop than 30% ( 30 percent ), this way every time the system memory drop to 30%, the code will quickly add another 90% (90 percent) to it.
**BEGIN****
*END***
Save the above file with any filename, in my case it's saved as memory_reset.php
How to use it?
===============
Just include saved file any header or footer file of your php script, this way if you want to disable it by adding a comment notation infront of the include statement.
e.g.
usage:
open your footer.php or the header.php and type the following:
include_once 'memory_reset.php';
Save it. You are good to go.
To disable it:
just add a comment notation infront of the include statement.
// include_once 'memory_reset.php';
Save it. Your setting is gone.
Let me know if this help?
Many website owners runs into this error and sometimes it seems as if it's fixed but just to find out that it is not permanently fixed.
If you are getting an error such as:
Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 8192 bytes) in /blah/blah
Below find out how to fix this, either temporarily or permanent solution
The reason is that the application/script running at the moment this error occurred does not have enough memory to complete the given task, hence reporting a fatal error message.
Solutions:
There are many quick fixes, but I preferred a permanent solution.
Solution #1.
If you have access to your php.ini file, the simple solution is to location the memory_limit = in your php.ini file and increase the limit.
e.g change :
Code:
memory_limit = 64M
to memory_limit = 256M
Solutioin #2 if you do not have access to system php.ini file (because you have a shared hosting account )
then you may be able to create a php.ini in your home directory and add the following to it:
Code:
memory_limit = 256M
In general this is a bad practice and should not be used on any production server.
Solution #3.
If your hosting allow you to modify .htaccess in the root folder of your website.
add the following to the top your .htaccess
Code:
php_value memory_limit 256M
Save it.
Permanent solution:
Since the more visitors on a website uses more resources, (memory etc) dynamically. Thus, a quick fix of increasing the memory will not be a good idea for a website that receive constant traffic.
I quickly wrote this piece of code to automate the process.
In my code, I set the memory not to drop than 30% ( 30 percent ), this way every time the system memory drop to 30%, the code will quickly add another 90% (90 percent) to it.
**BEGIN****
PHP Code:
<?php
//Declare at what percentage you would like your system memory to be restore
$lowMemory = int(0.30); // 0.10 = 10 percent, 0.50 = 50 percent
//Get initial system memory
$system_memory = ini_get('memory_limit');
if( preg_match('/^(\d+)(.)$/', $system_memory, $matches ){
if ( $matches[2] == 'M' ){
$system_memory = $matches[1] * 1024 * 1024; //MB
}else if ( $matches[2] == 'K' ){
$system_memory = $matches[1] * 1024;
}
//Set memory drop limit status to 30%
$mem_status = ( $system_memory * $lowMemory ) ;
//Add 90% to system memory if
$resetMemory = ( $mem_status + ( $system_memory * 0.90 ) );
}
//Check to is if your memory is getting lower than 30 percent, if yes, then add another 90 percent memory to the system memory
while( $mem_status ){
ini_set( 'memory_limit', $resetMemory );
}
?>
Save the above file with any filename, in my case it's saved as memory_reset.php
How to use it?
===============
Just include saved file any header or footer file of your php script, this way if you want to disable it by adding a comment notation infront of the include statement.
e.g.
usage:
open your footer.php or the header.php and type the following:
include_once 'memory_reset.php';
Save it. You are good to go.
To disable it:
just add a comment notation infront of the include statement.
// include_once 'memory_reset.php';
Save it. Your setting is gone.
Let me know if this help?