Magento setup:di:compile hits a memory limit

When setup:di:compile points at Composer’s memory-limit page, raise memory_limit on that PHP command. Do not assume COMPOSER_MEMORY_LIMIT covers Magento’s compile.

Last updated: October 27, 2026

bin/magento setup:di:compile is a PHP process. When it dies with a pointer to Composer’s memory-limit article, PHP refused an allocation. Give that Magento process a higher memory_limit. Setting COMPOSER_MEMORY_LIMIT only changes Composer’s own process, not every command Magento spawns.

Illustration of PHP stopping setup:di:compile because the memory limit was exhausted.
Illustration. PHP refused an allocation during setup:di:compile.

The message

The useful line is the one Composer prints when an allocation fails:

Check https://getcomposer.org/doc/articles/troubleshooting.md#memory-limit-errors

You may also see:

PHP Fatal error: Allowed memory size of ... bytes exhausted

If the paste is cut off before “Allowed memory size”, read var/log/system.log and the terminal scrollback before changing anything else. A compile can stop on a class error that only mentions Composer in the footer.

See the limit this PHP will use

php -r "echo ini_get('memory_limit'), PHP_EOL;"
php --ini

On Windows the same commands run in PowerShell. The number that matters is the one for this php.exe. A 4G value in an Apache php.ini does not apply to a CLI compile.

Composer 2 raises its own limit to 1.5G when the configured limit is lower, unless COMPOSER_MEMORY_LIMIT is set. The troubleshooting page says that change covers Composer and its immediate process. Child commands keep their own limit. setup:di:compile is one of those children when a plugin or script starts it from Composer, and it is the main process when you start it yourself.

Fix the compile you are running

From the Magento root, for this command only:

php -d memory_limit=2G bin/magento setup:di:compile

Windows:

php -d memory_limit=2G bin/magento setup:di:compile

Use 4G only if 2G still exhausts and the machine has the RAM. Avoid memory_limit=-1 on a small VPS. An unlimited compile can be killed by the host, which looks like a hang rather than a PHP error.

If the failure happens inside composer install or composer update itself:

COMPOSER_MEMORY_LIMIT=2G composer install

Windows PowerShell:

$env:COMPOSER_MEMORY_LIMIT = "2G"
composer install

Do not export an unlimited limit into a deploy hook and leave it there.

Make it stick for CLI only

Put the higher value in the CLI php.ini reported by php --ini:

memory_limit = 2G

Leave the web server lower if you want a runaway storefront request to die. Magento’s own recommendations for PHP settings are in the installation guide; a compile needs more than a single web request.

Run php -d memory_limit=2G -r "echo ini_get('memory_limit'), PHP_EOL;" with the same php binary that will run bin/magento. If that line does not print 2G, the host is ignoring -d and the compile command will fail the same way.

Check the result

A finished compile ends with a generated-code summary and returns you to the shell. Then:

php bin/magento module:status

generated/code contains factory and interceptor files. If the command exits non-zero, the memory change did not fix it. Read the new fatal. It may be a missing class, not memory.

If that does not work

  • The host enforces a cap below php.ini. php -r "echo ini_get('memory_limit');" after your change still shows the old value. Ask the host, or compile in CI where the limit is yours.
  • You are out of RAM, not out of the PHP setting. dmesg or the Windows Event Log shows the process was killed.
  • cPanel fork-bomb protection kills Composer. Composer’s docs call that out separately from memory_limit.
  • The next error is “Permission denied” or a missing class. Memory is no longer the blocker. Fix ownership or the missing package before running compile again.

Deleting generated/ and compiling again is safe on a maintenance window. It does not raise the memory limit.

References

On this page

Related solutions

Related Tools

Related articles

Need a tool for this ?

Open the free tools — no signup.

FAQs

Innovative Solutions For Modern Needs
Copyright © 2026 Yallasolve. all rights reserved.