18

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.

5
  • Show your code which you are used for save product. Commented Jan 18, 2018 at 12:10
  • You can set memory size to -1, so it will use the required memory size, setting is ini_set('memory_limit', -1); Commented Jan 18, 2018 at 12:26
  • Check it @SureshChikani Commented Jan 18, 2018 at 12:36
  • At which place I put memory size -1 ? Commented Jan 18, 2018 at 12:36
  • You can put it in index.php file at your root of magento. Commented Jan 18, 2018 at 12:37

7 Answers 7

55

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.

8
  • 1
    super. thank you. edit .user.ini helped me.
    – James M
    Commented Apr 27, 2018 at 10:37
  • 1
    Thank you very useful for me, I have added this and issue resolved Commented Mar 20, 2019 at 6:16
  • 1
    Explanation is great. Keep it up this approach
    – Jarnail S
    Commented Jun 29, 2019 at 7:35
  • 1
    First lit of commands worked for me. Thx!
    – Alex B
    Commented Nov 17, 2020 at 18:19
  • 1
    "php -d memory_limit=-1 bin/magento setup:di:compile" worked
    – Dipu R
    Commented Jul 21, 2021 at 9:01
7

A quick solution is

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

Try this, Its worked for me.

5

792723456 bytes mean 756 MB. This is the value that Magento suggests and is the default on their sample configs(nginx in particular).

Do try everything that @Pratik Oza suggested and let me point to another place. Are you using php-fpm or a similar CGI? If so then please do check for memory limits there too.

In my case i am using nginx + php-fpm and i set the memory_limit per project using the

php_value[memory_limit] = [N]MB

Also if you are using a CGI then try this. First stop your webserver then restart you CGI and then start your webserver again.

The is a random problem i faced once that had me doing the above.If i did not then it was like nginx set it's own values to anything passed to php-fpm without caring for the values set on php.ini or fpm.conf.

EDIT: I see the problem was solved by editing the .user.ini. That means that it was actually nginx that passed another value and could also be the case i described. Just pointing this one out. Also to anyone reading the other answers please do not use set_init('memory_limit',-1) EVER.

I've seen a developer of mine using this to test out a mysql query optimization and the script reached a 35gigs ram which kept increasing until i killed it after 10mins. Just set the value to a decent threshold until you get to the running point and leave it there or fix the values or the perspectives user.ini/fpm.conf/fascgi parameter.

1
php bin/magento setup:upgrade

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

php bin/magento cache:flush

php bin/magento cache:clean

It definitely be work.

0

This tripped me up when installing from archive instead of composer for magento 2.3. In my case, the settings I was changing was in the wrong place.

Was updating:

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

needed to update:

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

This is for both errors like:

PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted

and

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

Also make sure to enable rewrite: a2enmod rewrite if having issues with pages.

0

none of the above options not worked for me.

My server is nginx. It memory limit defined in /etc/nginx/magento.conf

open the file and search

fastcgi_param  PHP_VALUE "memory_limit=

inside the below block

location ~ (index|get|static|report|404|503|info|cleanopcache)\.php$ {

}

after found that line, the memory limit has to set as 6144M

fastcgi_param PHP_VALUE "memory_limit=6144M \n max_execution_time=600";

after that, it works fine.

you able to find how much memory limit set in your Magento projects via debug log.

make log with ini_get('memory_limit');

hope this helps you all.

0
php -d memory_limit=4G bin/magento setup:upgrade
    
php -d memory_limit=4G bin/magento setup:static-content:deploy -f
    
php -d memory_limit=4G bin/magento setup:di:compile
    
php -d memory_limit=4G bin/magento indexer:reindex
    
php -d memory_limit=4G bin/magento cache:flush
    
php -d memory_limit=4G bin/magento cache:clean 
    
php bin/magento cache:flush
    
php -d memory_limit=4G bin/magento catalog:image:resize

I hope your issue has been resolved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.