【问题标题】:Fatal error: Allowed memory size of 134217728 bytes exhausted Magento致命错误:允许的内存大小为 134217728 字节已用完 Magento
【发布时间】:2014-04-18 17:50:23
【问题描述】:

在 Magento 中我得到了这个错误。我知道这是一个内存问题,但我知道我的代码导致了这个问题。我该如何解决这样的问题?相同的代码运行了很长时间,突然就产生了问题。

致命错误:/home/wwwcruk/public_html/cas/app/design/frontend/default/cas/template/brandproduct/brand-listcar.phtml 中允许的内存大小为 134217728 字节已用尽(尝试分配 94115841 字节)第 60 行

我从 htaccess 分配了256M。当我从上面的文件中删除代码时,它可以工作。这是代码的一部分。这是导致问题的两行 ($manufacturer['label'][0] == $char):

 <?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 }?>

增加内存限制后我仍然收到错误。

[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

【问题讨论】:

  • @Stony 当我将此代码放入 index.php 文件 ini_set('memory_limit', '-1'); 时,我得到了 500 Internal Server Error
  • 典型的内存不足分配错误 (128M),Magento 需要 256M 或更多。更高版本,10k 目录,10k 客户,使其达到 512M 或更多。如果您正在运行外部 php 脚本,您还需要确保命令行 php 解释器也设置了内存限制。
  • @FiascoLabs 我已将内存增加到512MB,但仍然错误。您可以在我的问题中看到上述内容。

标签: php .htaccess magento memory-management


【解决方案1】:

有几件事 - 首先,您发布的代码不是问题。这只是 PHP 在达到内存限制时恰好正在执行的代码。

Magento 需要将 memory_limit 设置为 256 MB。我见过的大多数商店都运行 512 MB。这是因为 PHP 有一些已知的错误,当运行具有循环引用的大型对象集合/数组时,内存可能会“泄漏”。通常,当您在 PHP 中使用变量时,一旦不需要该变量,PHP 就会回收内存。因此,当我说“泄漏”时,我的意思是在某些情况下 PHP 不会回收内存,这可能导致 PHP 在瞬间使用大量内存。

不过,这似乎不是您的问题。你的错误

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

这是 134,217,728 字节 - 或 128 MB。您的 PHP 版本设置为使用 128MB,不是所需的 256MB。第一步应该弄清楚为什么会这样。

许多网络主机将其允许的内存限制为一定大小,以确保所有客户在共享资源上公平访问有限资源。这些相同的主机将禁用客户(即您)通过 .htaccess 设置内存限制。您通常需要通过php.ini 文件设置此值。

此外,此设置的shorthand notation 不是那么可靠。例如,如果您将限制设置为512M,您会很好,但使用512MB,PHP 会将字符串512MB 视为几个低数字。

希望有帮助!

【讨论】:

  • 感谢您的详细回复。我想知道为什么这个问题在我的本地系统上没有出现在服务器上。现在得到了128MB 的记忆提示。让我与托管服务提供商讨论这个问题。
【解决方案2】:

这看起来确实不正常。但是你能在你的问题中加入更多代码吗?如果 $char 或 $J 大于某个大数字,是否只是您的 while 循环没有中断。我们看不到你的整个循环试图做什么。

您要删除的代码是什么?不只是这两行吗?

我还要说 256M 可能不是一个巨大的内存量。如果你涨了,你还会达到新的限制吗?

***编辑以下cmets:

感谢您添加所有代码。我确定您已经分析了您的代码,但我在这里唯一的评论是关于 while 循环的功能。似乎$char 是一个整数,大概从0 开始计数,并且您将其与制造商标签或级别相匹配。很好,这个计划没有错。

但也许$char 应该在foreach 循环的一开始就设置回零:

<?php
    $i=0;
    $j=0;
    foreach ($manufacturers as $manufacturer):
      $char = 0;//or whatever its initial value should be
      //...
?>

因为如果$manufacturers$manufacturer 的顺序不是通过增加$manufacturer['label'][0] 来排序的,那么while 循环将永远不会停止,因为例如foreach 循环的第五次迭代$char 可能是8$manufacturer['label'][0] 可能是4

这有意义吗?我不知道您的代码,但是 while 循环没有替代退出,我认为即使不可能,在 while 循环中添加故障安全也是谨慎的。如:

<?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
?>

但是检查 $manufacturers 的顺序,并在某个时候将$char 重置为0,看看它是否能解决问题。如果您确实需要增加内存并且您的系统不能使用ini_set(),那么您可以编辑php.ini 文件并在那里设置内存限制(并且不要忘记重新启动您的网络服务器以应用 php.ini变化)。

【讨论】:

  • 感谢您的回复。我添加了有问题的主要代码。您可以检查是否有帮助。
  • 我已经尝试过你的代码,但没有用。我已将内存增加到512MB,但仍然出错。您可以在我的问题中看到上述内容。
  • 感谢您提供更多信息。 Alan 的回答是正确的,我现在看到您正在达到新的内存限制,所以我
  • 感谢您提供更多信息。 Alan 的回答是正确的,我现在看到您正在达到新的内存限制,所以看起来问题不止一个。您已达到新的内存限制,因此我们知道有额外的内存可用。你能告诉我们第 47 行的代码吗?尽管正如 Alan 所说,这不一定是问题所在,只是超出限制的线。您能否像我上面所做的那样分析您的代码,以考虑代码可能在哪里自行运行或递归?你能echo($char) 看看它是不是小于 10 什么的。
  • 好的,我的代码不起作用,但您是否至少将 $char 设置为 0 并在 while 循环上设置了故障安全?我没有运行我的代码,所以它可能有问题,但我认为我们需要知道 $char 在做什么,我很确定你希望它重置为 0 并且 while 循环应该有故障保护因此,无论您如何编写代码,您都应该在 while 中编写故障安全代码,然后分析代码以便理解例如 count($manufacturers) 以及 $char 的最大值和 $manufacturer['label'][0] 的最大值。您的$manufacturer['label'][0] 之一是否可能是非整数?
【解决方案3】:

我遇到了这个问题,这让我很困惑。它停在一个非常简单的 MySQL 查询上:

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();

我将查询重写为:

$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);

它解决了问题。

【讨论】:

    猜你喜欢
    • 2011-02-23
    • 2016-04-13
    • 2015-07-06
    • 2016-12-11
    • 2017-01-05
    • 2017-11-15
    • 2016-07-12
    • 1970-01-01
    • 2015-11-16
    相关资源
    最近更新 更多