Magento 2 is a resource-intensive application. The recommended memory_limit for Magento 2 is:
- 2G (2048M) for production environments.
- 3G (3072M) or more for development environments or large stores.
Check the Current PHP Memory Limit
To check the current PHP memory limit, create a phpinfo.php file:
phpinfo();
?>
Access it in your browser (http://yourdomain.com/phpinfo.php) and look for the memory_limit directive.
Alternatively, use the command line:
Update PHP Memory Limit
Modify php.ini
Locate your php.ini file
Open the file
Find the memory_limit directive and increase the value
Save and exit
Restart your web server
Modify .htaccess (Shared Hosting)
If you are on shared hosting, add this line to Magento 2 .htaccess file:
Modify PHP Command-Line Settings
Magento CLI commands often require a higher memory limit. Use the -d flag:
Modify in app/bootstrap.php
To temporarily override PHP settings for Magento 2:
- Open app/bootstrap.php.
- Add this line near the top:
Verify Changes
Run the following command to confirm the new memory limit:
You can also verify by running Magento CLI commands, such as:
Common Errors Related to Memory Limits
PHP Fatal Error: Allowed Memory Size Exhausted
If you encounter this error, it means the memory limit is too low. Increase it to 2G or more using the methods above.
Magento CLI Memory Exhaustion
When running Magento CLI commands like setup:upgrade or indexer:reindex, you might encounter memory issues. Always use:
- Recommended Memory Limit: At least 2G for Magento 2.
- Change in php.ini: For global settings.
- Use -d in CLI: For Magento CLI commands requiring more memory.
- Test Settings: Verify using phpinfo() or CLI.


