dongxu1029 2014-04-18 17:50
浏览 60
已采纳

致命错误:Magento允许的内存大小为134217728字节

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
  • 写回答

3条回答 默认 最新

  • dszsajhd237437 2014-04-18 20:32
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探