A quick solution for memory limit in Magento 2 command is directly adding memory limit inside command.
For Compilation command,
php -dmemory_limit=5G bin/magento setup:di:compile
For Deployment
php -dmemory_limit=5G bin/magento setup:static-content:deploy