Magento 2.2.2 – Allowed Memory Size Exhausted Error

fatal errormagento2magento2.2products

I am working in magento 2.2.2 and when I save product It will display this error.

Fatal error: Allowed memory size of 792723456 bytes exhausted (tried to allocate 184320 bytes) in /var/www/html/Demo/vendor/magento/framework/Profiler.php on line 153

Php version : 7.0

Magento : 2.2.2

I also changes apply in memory_limit = 16G in php.ini file and restart apache server. still, this issue not fixed.

<?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.'));
        }
    }
}

How to solve it ?

Please help me.

Best Answer

Try following commands:

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

If you are still facing issue then try following settings:

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

OR

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

Check current memory_limit value using CLI:

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

2. php -i | grep memory

If the issue are still persist then increase the memory limit (e.g., up to 2048M) in the following places:

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

Issue should be resolved by now, if it is still there then disable your custom module and check again.

Related Topic