When you run Magento 2 CLI command "bin/magento ..." and get the error:

Allowed memory size of xxxx bytes exhausted (tried to allocate xxx bytes)

You can easily fix it using this command instead of bin/magento:

php -dmemory_limit=-1 bin/magento ....

Examples:

php -dmemory_limit=-1 bin/magento setup:upgrade
php -dmemory_limit=-1 bin/magento setup:di:compile

This error is related to the memory limit configuration in PHP. Parameter -dmemory_limit=-1 allows to run PHP in the CLI without memory limits.

You can also change memory_limit configurations (php.ini file).