0

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
Adda
  • 215
  • 4
  • 18
  • possible duplicate of [PHP: Fatal Error: Allowed Memory Size of 134217728 Bytes Exhausted (CodeIgniter + XML-RPC)](http://stackoverflow.com/questions/561066/php-fatal-error-allowed-memory-size-of-134217728-bytes-exhausted-codeigniter) – René Höhle Apr 18 '14 at 18:04
  • @Stony when i put this code in index.php file `ini_set('memory_limit', '-1');` i got `500 Internal Server Error`. – Adda Apr 18 '14 at 18:10
  • Typical lack of memory allocated error (128M), Magento needs 256M or more. Later versions, 10k catalog, 10k customers, make it 512M or more. If you're running external php scripts, you also need to make sure the command line php interpreter has its memory limit set as well. – Fiasco Labs Apr 20 '14 at 05:24
  • @FiascoLabs i have increased the memory to `512MB` but still erro.You can see above in my question . – Adda Apr 21 '14 at 11:17

3 Answers3

4

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!

Alan Storm
  • 157,413
  • 86
  • 367
  • 554
  • Thanks for your details response.I was wondering that why this issue not occurring on my local system while on server does.Now got one hint of memory of `128MB`. Let me discuss the issue with hosting provider. – Adda Apr 20 '14 at 05:08
0

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).

Malachy
  • 1,540
  • 1
  • 11
  • 9
  • Thanks for your response.I added my main code in question.You can check if that can help. – Adda Apr 19 '14 at 06:04
  • I have tried your code but didn't work.i have increased the memory to `512MB` but still erro.You can see above in my question . – Adda Apr 21 '14 at 11:18
  • Thank you for the additional information. Alan's answer is correct and I see now that you arehitting your new memory limit so I – Malachy Apr 23 '14 at 12:19
  • Thank you for the additional information. Alan's answer is correct and I see now that you are hitting your new memory limit so it looks like there is more than one issue. You reached the new memory limit so we know that the additional memory is available. Can you tell us the code at line 47? Although as Alan says that is not necessarily the line with the problem, just the line that steps over the limit. Can you analyse your code as I have done above to think about where the code might be running away with itself or recursing? Can you `echo($char)` to see it is less than 10 or something. – Malachy Apr 23 '14 at 12:26
  • Okay, my code didn't work but are you at least setting `$char` to `0` and putting a fail-safe on the while loop? I didn't run my code so it might be buggy but I do think we need to know what $char is doing and I'm pretty sure you want it to be reset to `0` and the while loop should have a fail safe so however you code it you should code fail-safe into your while and then analyse the code so you understand eg `count($manufacturers)` and the maximum value of `$char` and the maximum value of `$manufacturer['label'][0]`. Is it possible that one of your `$manufacturer['label'][0]` is non-integer? – Malachy Apr 23 '14 at 12:31
  • got it .thanks it throw error when i have integer value in `$manufacturer['label'][0]`.so when i provide the string values in above array so it works perfect.Thanks for your hint. – Adda Apr 27 '14 at 11:24
0

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.

user3707094
  • 301
  • 2
  • 4