我正在尝试从命令行运行php脚本(magento reindexer脚本)。该脚本占用大量内存,因此我收到以下错误:PHP Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 72 bytes) in /home/karanta/www/karanta.fr/lib/Zend/Db/Adapter/Abstract.php on line 691
要解决此问题,我修改了/etc/php5/cli/php.ini
文件并设置了memory_limit = 2048M
。
要检查配置,我从cli运行一个包含phpinfo();
的脚本,我看到:memory_limit => 2048M => 2048M
,因此似乎正确考虑了配置。 ini_get('memory_limit);
也会返回2048M。
然而,当我重新运行reindex脚本时,我仍然得到PHP Fatal error: Allowed memory size of 536870912 bytes exhausted
,好像memory_limit仍然是512M。该脚本无法完成,我不知道如何扩充memory_limit以允许脚本完成。
编辑:我还尝试将指令ini_set("memory_limit", -1);
直接添加到PHP脚本中,它仍然挂起相同的PHP Fatal error: Allowed memory size of 536870912 bytes exhausted
。
额外信息:
服务器是运行Debian GNU / Linux 7.5且64GB RAM的ovh专用机器!
php -v
返回:
脚本执行期间PHP 5.6.12-1(cli)Copyright(c)1997-2015 PHP Group Zend Engine v2.6.0,版权所有(c)1998-2015 Zend Technologies Zend Technologies的Zend OPcache v7.0.6-dev,Copyright(c)1999-2015,
free -k -h
返回:
total used free shared buff/cache available
Mem: 62G 1,8G 48G 84M 12G 60G
Swap: 1,0G 0B 1,0G
ps -aux
返回:
karanta 25568 5.6 0.0 229484 41824 pts/1 S 07:54 0:04 php shell/indexer.php --reindex catalog_url
按照@ IgorGreg的推荐,我尝试使用Zend_Memory设置内存限制。我编写了这个php脚本,我从cli运行以替换shell / indexer.php脚本。
<?php
require_once 'app/Mage.php';
$app = Mage::app('admin');
umask(0);
$memoryManager = Zend_Memory::factory('none');
$memoryManager->setMemoryLimit(-1);
$process = Mage::getModel('index/process')->load(3);
$process->reindexAll();
?>
仍然遇到同样的错误。
答案 0 :(得分:6)
CLI模式下的Magento会解析.htaccess
文件,如果找到内存限制或其他任何PHP设置,则应用它们(是的,认真的......)
负责的代码位于shell/abstract.php
:
/**
* Parse .htaccess file and apply php settings to shell script
*
*/
protected function _applyPhpVariables()
{
$htaccess = $this->_getRootPath() . '.htaccess';
if (file_exists($htaccess)) {
// parse htaccess file
$data = file_get_contents($htaccess);
$matches = array();
preg_match_all('#^\s+?php_value\s+([a-z_]+)\s+(.+)$#siUm', $data, $matches, PREG_SET_ORDER);
if ($matches) {
foreach ($matches as $match) {
@ini_set($match[1], str_replace("\r", '', $match[2]));
}
}
preg_match_all('#^\s+?php_flag\s+([a-z_]+)\s+(.+)$#siUm', $data, $matches, PREG_SET_ORDER);
if ($matches) {
foreach ($matches as $match) {
@ini_set($match[1], str_replace("\r", '', $match[2]));
}
}
}
}
请勿通过.htaccess
设置内存限制和执行时间,使用php.ini
或虚拟主机配置。
答案 1 :(得分:0)
将memory_limit设置为-1表示没有内存限制。但是,如果在php.ini或.htaccess中设置此值,则脚本可能会在运行时覆盖该值。
为确保它生效,最好在脚本中使用ini_set("memory_limit", -1);
通常,在php.ini中将memory_limit设置为-1并不是一个好主意。在某些情况下,它可能会导致您的系统挂起。
Magento建立在Zend框架1上,它有自己的内存管理器。 documentation
你可以这样设置内存限制:
$memoryManager->setMemoryLimit(-1);
它不需要是-1,它可以是1,073,741,824,或任何整数,以字节为单位 我没试过。
答案 2 :(得分:0)
请按照以下步骤操作。
1。将以下内容更改为.htaccess以满足您的要求 php_value memory_limit 256M =&gt; php_value memory_limit 2048M
php indexer.php --reindex catalog_product_price
需要通过.htaccess增加内存限制,因为你可以在abstract.php中看到它将引用.htaccess配置。
如果您还有任何查询,请告诉我
由于