In Magento I got this error. I know this is a memory issue, but I know my code causing this. How can I solve such issue? The same code works for long time and suddenly it generates issue.
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 94115841 bytes) in /home/wwwcruk/public_html/cas/app/design/frontend/default/cas/template/brandproduct/brand-listcar.phtml on line 60
I allocated 256M
from htaccess. When I remove code from above file it works.This is the part of code. These are the two lines ($manufacturer['label'][0] == $char
) that are causing the issue:
<?php $i=0;$j=0;foreach ($manufacturers as $manufacturer): ?>
<?php if($i == 0 && $manufacturer['label'][0] == $char): $j++;?>
<li><span class="levelchar"><?php echo $char; ?></span></li>
<?php endif; ?>
<?php if($j>=$heightColumn):?>
</ul>
<ul class="level-brandul">
<?php $j=0; endif;?>
<?php while( $manufacturer['label'][0] != $char){ $char++;?>
<?php if( $manufacturer['label'][0] == $char): $j++; ?>
<li><span class="levelchar"><?php echo $char; ?></span></li>
<?php if($j>=$heightColumn):?>
</ul>
<ul class="level-brandul">
<?php $j=0; endif;?>
<?php endif; ?>
<?php }?>
I still got the error after increasing memory limit.
[Mon Apr 21 10:52:52 2014] [error] [client 82.94.176.140] in /home/wwwcruk/public_html/cas/app/design/frontend/default/cas/template/brandproduct/brand-listcar.phtml on line 47, referer: http://creationsautosport.co.uk/cas/index.php/catalog/product/view/id/58/s/bmw-m-power-silver-number-plate-surrounds/category/4/
[Mon Apr 21 10:49:50 2014] [error] [client 82.94.176.140] Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 504104961 bytes) in /home/wwwcruk/public_html/cas/app/design/frontend/default/cas/template/brandproduct/brand-listcar.phtml on line 47, referer: http://creationsautosport.co.uk/cas/index.php/catalog/product/view/id/58/s/bmw-m-power-silver-number-plate-surrounds/category/7/
[Mon Apr 21 10:47:08 2014] [error] [client 82.94.176.140] Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 504104961 bytes) in /home/wwwcruk/public_html/cas/app/design/frontend/default/cas/template/brandproduct/brand-listcar.phtml on line 47, referer: http://creationsautosport.co.uk/cas/index.php/catalog/product/view/id/58/s/bmw-m-power-silver-number-plate-surrounds/category/9/
[Mon Apr 21 10:46:56 2014] [error] [client 39.47.121.31] Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 496762881 bytes) in /home/wwwcruk/public_html/cas/app/design/frontend/default/cas/template/brandproduct/brand-listcar.phtml on line 47
This seems irregular certainly. But can you put more code in your question? Is it just that your while loop doesn't have a break if $char or $J is greater than some large number. We can't see what your whole loop is trying to do.
What is the code you are removing? It isn't just these two lines is it?
I would also say that 256M maybe isn't a huge amount of memory. If you up it do you still hit the new limit?
***EDIT following comments below:
Thank you for adding all the code. I'm sure you have analysed your code, but my only comment from here is about how the while
loop functions. It seems $char
is an integer number presumably counting up from 0
and you are matching that to a manufacturer label or level. Good, nothing wrong with that plan.
But perhaps $char
should be set back to zero at the very start of the foreach
loop:
<?php
$i=0;
$j=0;
foreach ($manufacturers as $manufacturer):
$char = 0;//or whatever its initial value should be
//...
?>
Because if the order of $manufacturer
in $manufacturers
is not ordered by increasing $manufacturer['label'][0]
then the while loop will never stop because on eg the fifth iteration of the foreach
loop $char
might be 8
and $manufacturer['label'][0]
might be 4
.
Does that make sense? I don't know your code, but the while loop has no alternative exit and I think even if it is impossible, adding a fail-safe to the while loop would be prudent. Such as:
<?php
$bailAt = PHP_INT_MAX - 1;//this is a very large number => wait a long time to bail
$bailAt = 32000; //if you have over 32000 manufacturers in Magento you probably have other issues too
while( ($manufacturer['label'][0] != $char) && ($char < $bailAt) ){
$char++;
//...
}//end while
if ($char==$bailAt){
//recover gracefully
echo("<!-- there was an error building the manufacturer levels (reached maximum levels) -->");
echo("</ul>"); //you might not need that
}//end if
?>
But check the ordering of $manufacturers and reset $char
to 0
at some point and see if it fixes things. If you do need to increase the memory and your system can't use ini_set()
then you can edit php.ini
file instead and set the memory limit there (and don't forget to restart your web server to apply the php.ini changes).
A couple of things — first, the code you posted isn't the problem. That's just the code PHP happens to be executing when it hits the memory limit imposed.
Magento requires a memory_limit
set to 256 MB. Most shops I've seen run with 512 MB. That's because PHP has a few known bugs where memory can "leak" when running through large collections/arrays of objects with circular references. Normally when you use a variable in PHP, once that variable isn't needed PHP reclaims the memory. So when I say "leak", what I mean is there's situations where PHP won't reclaim the memory, which can lead to PHP using a large amount of ram for a split second.
However, — that doesn't seem to be your problem. Your error
Fatal error: Allowed memory size of 134217728 bytes exhausted
That's 134,217,728 bytes — or 128 MB. You version of PHP is set to use 128MB, not the required 256MB. Step one should be figuring out why this is.
Many web hosts cap their allowed memory to a certain size in order to ensure fair access to limited resource for all their customers on shared resources. These same hosts will disable customers (i.e. you) setting the memory limite via .htaccess. You'll often need to set this value via the php.ini
file.
Also, the shorthand notation of this setting isn't that robust. For example, if you set the limit to 512M
, you'll be good, but use 512MB
, and PHP will treat the string 512MB
as a few low number.
Hope that helps!
I had this problem which really puzzled me. It stopped at a very simple MySQL query:
stmt = $link -> prepare("SELECT 'content' FROM `pages` WHERE `ID`=? LIMIT 1")) {
$stmt -> bind_param("s", $pageID);
$stmt -> execute();
$stmt -> bind_result($result);
$stmt -> fetch();
if (!$result) {
return FALSE;
}
$stmt -> close();
I rewrote the query to this:
$sql_content = "SELECT `content` FROM `pages` WHERE `ID`=".mysql_real_escape_string($pageID). " LIMIT 1";
$q_content = mysqli_query($link, $sql_content);
$r_content = mysqli_fetch_assoc($q_content);
It solved the problem.