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.

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.
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.
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.
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.
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.
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.dmesg or the Windows Event Log shows the process was killed.memory_limit.Deleting generated/ and compiling again is safe on a maintenance window. It does not raise the memory limit.
Only if Composer itself is the process that ran out of memory. The compile command needs php -d memory_limit on bin/magento.
Not on a small server. An unlimited compile can be killed by the host. Use 2G, then 4G only if the machine has the RAM.
php --ini shows which file you edited. If the value is still the old one, the host is capping it below php.ini.