Php memory Limit Exceed, while runing Magento 2 Commands

Php memory Limit Exceed, while runing Magento 2 Commands

September 7, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Php memory Limit Exceed, while runing Magento 2 Commands

Resolving PHP Memory Limit Exceed Errors in Magento 2

When working with Magento 2, you may encounter PHP memory limit exceed errors during deployment or when running cron jobs. This issue is quite common due to the resource-intensive nature of Magento’s operations. Let me guide you through diagnosing and resolving this problem efficiently.

Recognizing the Error

The most common error message looks like this:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 5096848 bytes) in /var/www/html/magento2/lib/internal/Magento/Framework/App/State.php on line 0

Understanding the Cause

The error occurs because Magento is trying to allocate more memory than the current PHP limit allows. This happens frequently during static content deployment, index management operations, or when running complex cron jobs.

Identifying the Memory Limit

First, you need to locate your current memory limit setting in PHP. It’s usually defined in your php.ini file:

memory_limit = 128M

You can also check it via a simple PHP command:

<?php echo ini_get('memory_limit'); ?>

Increasing the Memory Limit

To resolve this issue, you have two options: increase the memory limit or optimize your code.

Option 1: Increase the Memory Limit

For development purposes, you can temporarily increase the memory limit in your php.ini file:

memory_limit = 256M

Alternatively, you can use the -d memory_limit=256M flag when running commands from the command line.

Note: Increasing the memory limit significantly isn’t always a viable long-term solution, especially for production environments. It can lead to other issues like high server loads or slower response times.

Option 2: Optimize Your Code

Before blindly increasing memory limits, consider optimizing your code:

  1. Avoid using too many objects: Each object consumes memory. Try to use data structures efficiently and avoid unnecessary instantiations.

  2. Minimize the size of arrays: Smaller arrays require less memory than large ones. Break down complex operations into smaller steps if possible.

  3. Use string functions properly: String concatenation can be expensive in terms of memory usage, especially with large strings. Use the sprintf function for formatted output instead.

  4. Limit cache sizes: If you’re using a caching system like Redis or Memcached, ensure their memory usage doesn’t exceed your available resources.

Best Practices from LaravelCompany

At LaravelCompany, we recommend applying the following best practices to minimize memory issues:

  1. Use a staging environment: Always perform heavy operations like static content deployment or index reindexing in a development or staging environment before moving them to production.

  2. Optimize your database: Regularly check for and optimize any slow queries that may be causing high resource usage.

  3. Monitor your server: Keep an eye on your server’s CPU and memory utilization using tools like New Relic or Datadog. This can help you identify areas of high load before they cause critical errors.

Conclusion

Remember, increasing the memory limit should be considered a last resort. Always strive to optimize your code and operations first. If you find yourself frequently exceeding memory limits, it might indicate that an upgrade to more robust hardware is needed.

For a detailed guide on Magento 2 performance optimization, I recommend checking out our post at LaravelCompany. It provides step-by-step instructions for improving your store’s speed and resource efficiency.

Magento 2 Indexing Performance Configuration

Our network