Skip to content
Home » News » Magento 2 Error: Allowed Memory Size of Bytes Exhausted

Magento 2 Error: Allowed Memory Size of Bytes Exhausted

If you are running an online store with Magento 2, most likely sooner or later, you will run into technical issues. One such common problem is the Allowed memory size of bytes exhausted error, which might look like this:

Allowed memory size of bytes exhausted error in Magento 2
Allowed memory size of xxxx bytes exhausted (tried to allocate xxx bytes)

This error often happens when running Magento commands like setup:di:compile, setup:static-content:deploy, or when installing libraries using Composer. Below, you can read what causes it and how you can fix it.


Why Does Allowed Memory Size Exhausted Error in Magento 2 Happen?

The error usually occurs because Magento tries to use more memory than what’s allowed by your server’s PHP settings. Here are the main reasons:

  1. Low PHP Memory Limit: The PHP memory_limit setting controls how much memory a script can use. If an operation needs more memory, for example, updating thousands of products or importing data, this error will show up. 
  2. Too Many Extensions or Customizations: Magento is highly customizable, but using too many extensions or poorly optimized custom code can lead to a memory usage increase and hence trigger the error.
  3. Composer Commands: Installing libraries or updating dependencies with Composer also needs a lot of memory, which might exceed the allowed limit.

How to Fix the Allowed Memory Size of Bytes Exhausted Error

Here are some simple ways to fix the Allowed memory size exhausted error in Magento 2:

1. Increase Memory Limit in PHP Settings

  1. Find the php.ini file (usually in the etc/ folder).

Look for the line:

memory_limit = 128M 
  1. Change it to a higher value, for example:
memory_limit = 512M
  1. You can set it to 1G or even higher if needed.
  2. Restart your server for the changes to apply.

2. Run Magento Commands with More Memory

Also, you can increase the memory limit temporarily when running Magento commands by using the -d option:

php -d memory_limit=1G bin/magento setup:di:compile
php -d memory_limit=1G bin/magento setup:static-content:deploy

If you want to remove the memory limit entirely, use -1:

php -d memory_limit=-1 bin/magento setup:upgrade
php -d memory_limit=-1 bin/magento setup:di:compile

3. Increase Memory for Composer

When you are installing or updating libraries with Composer, you can increase the memory limit like this:

php -d memory_limit=-1 composer require name-of-your-library

If you need to find the path of Composer, you may use this command:

which composer

Then, include that path in your command if necessary.


4. Optimize Your Store

If this error happens often, it’s a sign that your store uses too much memory. Here’s how to fix that:

  • Recheck Your Extensions: Remove unnecessary or duplicate extensions.
  • Improve Custom Code: Check for poorly written customizations and optimize them.
  • Break Down Large Tasks: Split large operations (such as, for example, imports/exports) into smaller parts/steps.

How to Prevent This Error in the Future

  • Monitor Server Usage: Keep an eye on your server’s memory and adjust settings when requested.
  • Optimize Your Magento Setup: Regularly audit extensions and code to reduce memory consumption.
  • Use Best Practices: Follow Magento’s guidelines for customizations and updates.

Conclusion

The Allowed memory size exhausted error in Magento 2 is common but easy to fix. You can increase the memory limit in PHP settings or temporarily adjust it when running commands. To avoid the problem in the future, make sure your store’s extensions and customizations are optimized. If you’re still having trouble, contact a Magento expert for help.

Author

Leave a Reply

Your email address will not be published. Required fields are marked *