Encountered the following error when building my Android app with Android Studio:

:app:dexDebug
AGPBI: {"kind":"simple","text":"UNEXPECTED TOP-LEVEL ERROR:","sources":[{}]}
AGPBI: {"kind":"simple","text":"java.lang.OutOfMemoryError: GC overhead limit exceeded","sources":[{}]}

FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:dexDebug'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/bin/java'' finished with non-zero exit value 3

I didn't see this problem before upgrading Android Studio and incorporating some new libraries. Anyway I learned how to fix it with the following snippets in root Gradle build script:

dexOptions {
  javaMaxHeapSize "2g"
}

Reference to DexOptions in Android Plugin DSL Reference for details.