How to fix java.lang.OutOfMemoryError GC overhead limit exceeded error
April 28, 2022
How to fix java.lang.OutOfMemoryError: GC overhead limit exceeded error:
I was facing this error while building an Android project. This error might appear on building a react native, Android, or Flutter project. GC stands for Garbage collector. This error is thrown if the Garbage collector spents too much time.
Error:: D8: java.lang.OutOfMemoryError: GC overhead limit exceeded
FAILURE: Build failed with an exception.
*What went wrong
Execution failed for task ':app:mergeExtDexDebug'.
> Could not resolve all files for configuration ':app:debugRuntimeClasspath'.
What is garbage collector:
Garbage collector removes unused objects from memory. If the app creates a lot of objects, garbage collector runs to clean the memory. Garbage collector consumes a lot of CPU. Also, frequent running of garbage collector might pause the app.
Cause of this error:
This is an OutOfMemoryError. This error is thrown if too much time is spent by the garbage collector on cleaning up the memory. If it can only recover less than 2% of heap space and if more than 98% of time is spent on garbage collection, it will throw this OutOfMemoryError exception.
The heap is small in size. So, it will prevent the app from running for a long time with little to no progress on garbage collection.
Fix for this issue:
This issue can be fixed by editing the android/gradle.properties
file. We have to add the below property:
org.gradle.jvmargs=-Xmx4096M
If gradle.properties file is not there in the android folder, you can create it.