在Magento中,我得到了这个错误。我知道这是一个内存问题,但我知道我的代码导致了这个问题。我该如何解决此类问题?相同的代码可以长时间工作,突然就会出现问题。
致命错误:允许的内存大小为 134217728 字节耗尽(尝试分配 94115841 字节(在第 60 行的/home/wwwcruk/public_html/cas/app/design/frontend/default/cas/template/brandproduct/brand-listcar.phtml 中
我从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
有几件事——首先,你发布的代码不是问题。 这只是PHP碰巧在达到施加的内存限制时执行的代码。
Magento需要将memory_limit
设置为256 MB。 我见过的大多数商店都运行512 MB。 这是因为 PHP 有一些已知的错误,当运行具有循环引用的大型对象集合/数组时,内存可能会"泄漏"。 通常,当您在 PHP 中使用变量时,一旦不需要该变量,PHP 就会回收内存。 因此,当我说"泄漏"时,我的意思是在某些情况下,PHP不会回收内存,这可能导致PHP在一瞬间使用大量RAM。
然而,这似乎不是你的问题。 你的错误
致命错误:允许的内存大小 134217728 字节已耗尽
这是 134,217,728 字节 — 或 128 MB。 您的 PHP 版本设置为使用 128MB,而不是所需的 256MB。 第一步应该是弄清楚为什么会这样。
许多Web主机将其允许的内存限制为一定大小,以确保共享资源上的所有客户都可以公平地访问有限的资源。 这些相同的主机将禁用客户(即您(通过 .htaccess 设置内存限制。 通常需要通过php.ini
文件设置此值。
此外,此设置的速记符号也不是那么可靠。 例如,如果你将限制设置为 512M
,你会很好,但使用 512MB
,PHP 会将字符串512MB
视为几个小数字。
希望对您有所帮助!
似乎很不正常。但是你能在你的问题中加入更多的代码吗?只是如果$char或$J大于某个大数字,您的 while 循环没有中断。我们无法看到您的整个循环试图做什么。
您要删除的代码是什么?不就是这两条线吧?
我还要说,256M可能不是一个巨大的内存量。如果你上升了,你还会达到新的限制吗?
在下面编辑以下评论:
感谢您添加所有代码。我相信你已经分析了你的代码,但我在这里唯一的评论是关于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
文件并在那里设置内存限制(并且不要忘记重新启动您的 Web 服务器以应用 php.ini 更改(。
我遇到了这个问题,这真的让我感到困惑。它停止在一个非常简单的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);
它解决了问题。