Magento uses dependency injection (DI) to manage classes and objects. If DI is not compiled in production, the system falls back to runtime resolution. This is slower, consumes more memory, and may expose error traces if class generation fails.
Compiling DI before deployment improves performance, reduces runtime errors, and ensures that no debug artifacts are created on the fly in the production server.
# Check if generated/code directory has compiled classes
ls -l generated/code
# Run DI compile check
bin/magento setup:di:compile --dry-run
Generated code exists in generated/code/ and compilation passes without errors.
bin/magento setup:di:compile
generated/ directory before compilation to avoid stale classes:
rm -rf generated/code/* generated/metadata/*
# Production without DI compiled
generated/code/ is empty
Runtime tries to create classes (slow and risky)
# Production with DI compiled
generated/code/Magento/Catalog/...
# Classes pre-generated, system runs faster