Magento 2.2.2:已用完允许的内存大小为792723456字节(尝试分配184320字节)


11

我在magento 2.2.2中工作,当我保存产品时,它将显示此错误。

致命错误:在第153行的/var/www/html/Demo/vendor/magento/framework/Profiler.php中,耗尽了允许的内存大小为792723456字节(尝试分配184320字节)

Php version : 7.0

Magento : 2.2.2

我也改变适用于memory_limit = 16Gphp.ini文件并重新启动Apache服务器。仍然,这个问题没有解决。

<?php
namespace Abc\Xyz\Observer;

use Magento\Framework\Event\ObserverInterface;
use \Magento\Store\Model\StoreManagerInterface;

class ProductSaveAfter implements ObserverInterface
{
    protected $_objectManager;
    protected $messageManager;
    protected $_productmFactory;
    protected $_productdFactory;
    protected $_productsFactory;
    protected $_eavConfig;
    protected $sStore;
    protected $scopeConfig;
    protected $_helper;
    protected $storeManager;

    public function __construct(
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Framework\ObjectManagerInterface $objectManager,
        \Magento\Framework\App\RequestInterface $request,
        \Magento\Eav\Model\Config $eavConfig,
        \Abc\Xyz\Helper\Data $helper,
        StoreManagerInterface $storeManager,
        \Abc\Xyz\Model\productmFactory $productmFactory,
        \Abc\Xyz\Model\ProductdFactory $ProductdFactory,
        \Abc\Xyz\Model\productsFactory $productsFactory,
        \Abc\Xyz\Model\sStoreFactory $sStoreFactory,
        \Magento\Framework\Message\ManagerInterface $messageManager
    ) {
        $this->_objectManager = $objectManager;
        $this->_request = $request;
        $this->_helper = $helper;
        $this->scopeConfig = $scopeConfig;
        $this->_eavConfig = $eavConfig;
        $this->_productmFactory = $productmFactory;
        $this->_productdFactory = $ProductdFactory;
        $this->_productsFactory = $productsFactory;
        $this->sStore = $sStoreFactory;
        $this->storeManager = $storeManager;
        $this->messageManager = $messageManager;
    }
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $product = $observer->getEvent()->getProduct();
        $id = $product->getId();
        $data = $this->_request->getParams();
        $currentWebsite = $this->storeManager->getStore()->getWebsiteId();
        try
        {
            if ($id) {
                $YG14 = $this->_request->getParam('YG14');
                $YG14_size = $this->_request->getParam('YG14_size');

                $mI = $this->_productmFactory->create()->getCollection();
                $mI->addFieldToFilter('product_id', $id);
                /** Delete all records*/
                if ($YG14 != "") {
                    if (!empty($mI)) {
                        foreach ($mI as $mtl) {
                            $mI = $this->_productmFactory->create()->load($mtl->getId());
                            $mI->delete();
                        }
                    }
                }

                if ($YG14 != "") {
                    $mISave = $this->_productmFactory->create();
                    $mISave->setProductId($id);
                    $mISave->setMtlWeight($YG14);
                    $mISave->setMtlType('YG14');
                    $mISave->setSizeIncrementWeightDiff($YG14_size);
                    $mISave->save();
                }
                $dD = $this->_request->getParam('d');
                if ($dD) {
                    $dInfoDelete = $this->_productdFactory->create()->getCollection()->addFieldToFilter('product_id', $id);
                    foreach ($dInfoDelete as $key => $dia) {
                        $dInfo = $this->_productdFactory->create()->load($dia->getId());
                        $dInfo->delete();
                    }
                    foreach ($dD as $d) {
                        $dS = $this->_productdFactory->create();
                        $dS->setProductId($id);
                        if (array_key_exists('d_s', $d)) {
                            $dS->setDS($d['d_s']);
                        }
                        if (array_key_exists('d_w', $d)) {
                            $dS->setDW($d['d_w']);
                        }
                        if (array_key_exists('d_q', $d)) {
                            $dS->setDQ($d['d_q']);
                        }
                        if (array_key_exists('d_s_t', $d)) {
                            $dS->setDST($d['d_s_t']);
                        }
                        $dS->save();
                    }
                } else {
                    $deld = $this->_request->getParam('deld');
                    if ($deld != "") {
                        $delD = explode(",", $deld);
                        foreach ($delD as $delDIds) {
                            $dD = $this->_productdFactory->create()->load($delDIds);
                            $dD->delete();
                        }
                    }
                }
                $sD = $this->_request->getParam('st');
                if ($sD) {
                    foreach ($sD as $sts) {
                        $ssSave = $this->_productsFactory->create();
                        if (isset($sts['st_ids'])) {
                            $ssSave->setId($sts['st_ids']);
                        }
                        $ssSave->setProductId($id);
                        if (array_key_exists('st_c', $sts)) {
                            $ssSave->setStC($sts['st_c']);
                        }
                        if (array_key_exists('st_sett', $sts)) {
                            $ssSave->setStSett($sts['st_sett']);
                        }
                        if (array_key_exists('st_s', $sts)) {
                            $ssSave->setStS($sts['st_s']);
                        }
                        if (array_key_exists('st_w', $sts)) {
                            $ssSave->setStW($sts['st_w']);
                        }
                        if (array_key_exists('st_q', $sts)) {
                            $ssSave->setStQ($sts['st_q']);
                        }
                        $ssSave->save();
                        $sStore = $this->sStore->create();
                        $sStoreExists = $sStore->getCollection()->addFieldToFilter('st_id', $ssSave->getId())->addFieldToFilter('website_id', $currentWebsite);
                        if (!empty($sStoreExists->getData())) {
                            foreach ($sStoreExists as $value) {
                                $sStore->load($value->getId());
                                $sStore->setId($value->getId());
                                $sStore->setProductId($id);
                                $sStore->setPrice($sts['price']);
                                $sStore->save();
                            }
                        } else {
                            $sStore->setWebsiteId($currentWebsite);
                            $sStore->setProductId($id);
                            $sStore->setStId($ssSave->getId());
                            if (array_key_exists('price', $sts)) {
                                $sStore->setPrice($sts['price']);
                            }
                            $sStore->save();
                        }
                    }
                    $delSt = $this->_request->getParam('delSt');
                    if ($delSt != "") {
                        $delS = explode(",", $delSt);
                        foreach ($delS as $delSIds) {
                            $dDelete = $this->_productsFactory->create()->load($delSIds);
                            $dDelete->delete();
                        }
                    }
                } else {
                    $delSt = $this->_request->getParam('delSt');
                    if ($delSt != "") {
                        $delS = explode(",", $delSt);
                        foreach ($delS as $delSIds) {
                            $dDelete = $this->_productsFactory->create()->load($delSIds);
                            $dDelete->delete();
                        }
                    }
                }

                $prices = $this->_helper->defaultPriceCalculate($id, $product, $currentWebsite);
                $product->setWebsiteId($currentWebsite)->setPrice($prices['finalPrice']);
                if ($prices['discountedPrice'] != 0) {
                    $product->setSpecialPrice($prices['discountedPrice']);
                }
                $product->save();

            }
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            $this->messageManager->addError($e->getMessage());
        } catch (\RuntimeException $e) {
            $this->messageManager->addError($e->getMessage());
        } catch (\Exception $e) {
            $this->messageManager->addException($e, __('Something went wrong while saving the post.'));
        }
    }
}

怎么解决呢?

请帮我。


显示您用于保存产品的代码。
Suresh Chikani

您可以将内存大小设置为-1,这样它将使用所需的内存大小,设置为ini_set('memory_limit',-1);
Himmat Paliwal

检查@SureshChikani
Rohan Hapani

我在哪里放置内存大小-1?
罗汉·哈帕尼

您可以将其放在magento根目录的index.php文件中。
Himmat Paliwal

Answers:


33

尝试以下命令:

php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy
php bin/magento setup:di:compile
php bin/magento cache:flush
php bin/magento cache:clean

//To run command forcefully
php -f bin/magento

//To run command with memory limit 4G
php -d memory_limit=4G bin/magento

//To run command with max memory limit
php -d memory_limit=-1 bin/magento

如果仍然遇到问题,请尝试以下设置:

  1. [Magento2-root-folder] /index.php
ini_set('memory_limit',256);

OR

ini_set('memory_limit','-1');

使用CLI检查当前的memory_limit值:

1. php -r "echo ini_get('memory_limit').PHP_EOL;"

2. php -i | grep memory

如果问题仍然存在,请在以下位置增加内存限制(例如,最大2048M):

  1. [Magento2-root-folder] /。user.ini
memory_limit = 768M
  1. [Magento2-root-folder] /。htaccess(2个地方)
php_value memory_limit 768M
  1. [Magento2-root-folder] /pub/.user.ini
memory_limit = 768M
  1. [Magento2-root-folder] /pub/.htaccess(2个地方)
php_value memory_limit 768M

现在应该解决问题,如果仍然存在,请禁用自定义模块,然后再次检查。


1
超。谢谢。编辑.user.ini帮助了我。
James M

@JamesM-乐于帮助。继续提供帮助:)
Pratik Oza

谢谢您,对我非常有用,我添加了此内容,并解决了问题
Pradip Garchar

解释很棒。请继续保持这种方式

@PratikOza面临类似的错误,因此使用您的建议我还必须运行这些命令?测试它是否正常工作php bin / magento setup:static-content:deploy -f,php bin / magento cache:flush
Ahmer Saeed 19'Aug

7

一个快速的解决方案是

php -dmemory_limit=6G bin/magento setup:static-content:deploy

试试这个,它为我工作。


3

792723456字节平均756 MB。这是Magento建议的值,并且是其示例配置(尤其是nginx)的默认值。

尝试@Pratik Oza建议的所有内容,然后让我指向另一个地方。您使用的是php-fpm还是类似的CGI?如果是这样,请也检查那里的内存限制。

就我而言,我使用的是nginx + php-fpm,并且我使用

php_value [memory_limit] = [N] MB

另外,如果您使用的是CGI,请尝试此操作。首先停止您的Web服务器,然后重新启动CGI,然后再次启动您的Web服务器。

我曾经做过上面的工作,这是我遇到的一个随机问题。

编辑:我看到通过编辑.user.ini解决了问题。这意味着实际上是nginx传递了另一个值,也可能是我描述的情况。只是指出这一点。另外,对于阅读其他答案的任何人,请勿使用set_init('memory_limit',-1)EVER。

我见过我的开发人员使用它来测试mysql查询优化,并且脚本达到了35gigs的内存,该内存一直在增加,直到10分钟后我将其杀死。只需将值设置为适当的阈值,直到到达运行点并将其留在此处或固定值或透视图user.ini / fpm.conf / fascgi参数即可。


1
php bin/magento setup:upgrade

php bin/magento setup:static-content:deploy -f

php bin/magento cache:flush

php bin/magento cache:clean

绝对可以。


0

从存档而不是magento 2.3的composer进行安装时,这让我大跌眼镜。就我而言,我更改的设置在错误的位置。

正在更新:

vi /etc/php/7.2/cli/php.ini

需要更新:

vi /etc/php/7.2/apache2/php.ini

这是针对两个错误的,例如:

PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted

PHP Fatal error:  Maximum execution time of 30 seconds exceeded in....

a2enmod rewrite如果页面有问题,也请确保启用重写。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.