34

I get an java.lang.OutOfMemoryError: GC overhead limit exceeded when run gradle on Android 1.4 ... these are my depedencies :

dependencies {
    compile project(':android-crop')
    compile project(':RTEditor-Toolbar')

        compile files('libs/apache-mime4j-0.6.jar')
        compile files('libs/httpmime-4.1.3.jar')
    /*    compile files('libs/httpcore-4.4.1.jar')*/
    compile files('libs/jetbrains-annotations.jar')
    compile files('libs/pinchzoom.jar')
    compile files('libs/gcm.jar')
    compile 'com.google.android.gms:play-services:7.8.0'
    compile 'com.android.support:multidex:1.0.0'
    compile 'com.android.support:appcompat-v7:22.2.1'
    compile 'com.android.support:cardview-v7:22.2.1'
    compile 'com.android.support:design:22.2.1'
    compile 'com.android.support:recyclerview-v7:22.2.1'
    compile 'com.android.support:support-v4:22.2.1'
    //three party library
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.mcxiaoke.volley:library:1.0.18'
    compile 'com.vinaysshenoy:mugen:1.0.1'
    compile 'com.github.clans:fab:1.5.5'
    compile 'com.nineoldandroids:library:2.4.0'
    compile 'com.github.curioustechizen.android-ago:library:1.3.0'
    compile 'com.squareup.okio:okio:1.5.0'
    compile 'com.squareup.okhttp:okhttp:2.4.0'
    compile 'com.squareup.okhttp:okhttp-urlconnection:2.4.0'
    compile('com.crashlytics.sdk.android:crashlytics:2.5.1@aar') {
        transitive = true;
    }

How can this be fixed?

TaW
  • 48,779
  • 8
  • 56
  • 89
Amay Diam
  • 2,299
  • 6
  • 30
  • 53
  • 1
    add `dexOptions { // Prevent OutOfMemory with MultiDex during the build phase javaMaxHeapSize "4g" }` in your **build.gradle** – M D Aug 21 '15 at 05:36

7 Answers7

78

Add this to your android closure(build gradle):

 dexOptions {
        javaMaxHeapSize "4g"
 }

This will solve your problem. Still, if you face problem see the following link

GC overhead limit exceeded error

Killer
  • 3,574
  • 6
  • 35
  • 51
King of Masses
  • 16,881
  • 4
  • 57
  • 75
  • 2
    `incremental true` is deprecated now, and have no effect on build process. So, can be ignored. – Narendra Singh Nov 13 '16 at 22:23
  • 1
    didn't help, I set it to 12gigs and still got the error – Ábrahám Endre Jan 25 '17 at 14:52
  • @ÁbrahámEndre can you try this http://stackoverflow.com/questions/36643102/android-studio-2-1-instant-run-java-lang-outofmemoryerror-gc-overhead-limit-exc – King of Masses Jan 25 '17 at 16:22
  • already tried that, I've started the IDE with -Xmx4096 and even turned out the GCOverhead, did the same for gradle.properties too and still ran out. Problem is not present if I remove facebook SDK. – Ábrahám Endre Jan 26 '17 at 09:56
  • 9
    just for clarification, add it to `app/build.gradle` inside the `android { }` object. – pmont May 22 '18 at 17:46
47

This is what I suggest:

Add this to your "gradle.properties" file:

org.gradle.jvmargs=-Xmx4096m -XX:MaxPermSize=4096m -XX:+HeapDumpOnOutOfMemoryError

Also, read this article. You might be able to make the building a bit faster, by adding a combination of those:

org.gradle.daemon=true
org.gradle.parallel=true
org.gradle.configureondemand=true
android developer
  • 106,412
  • 122
  • 641
  • 1,128
5

This worked for me !! Just add the following lines into "gradle.properties" file

org.gradle.jvmargs=-Xmx4096m -XX:MaxPermSize=4096m -XX:+HeapDumpOnOutOfMemoryError
org.gradle.daemon=true
org.gradle.parallel=true
org.gradle.configureondemand=true
abhi
  • 228
  • 3
  • 9
  • This answer was given by @android-developer three years previously and with more detail - don't copy other people's answers please. – donturner Aug 11 '20 at 21:05
5

In my case, OutOfMemoryError is come from this error. Starting a Gradle Daemon, 1 busy and 6 stopped Daemons could not be reused, use --status for details

To solve this. I use ./gradlew --stop or ./gradle --stop. If still not solved which mean the gradle process is lock (this happen after OutOfMemoryError).

Go to /users/[username]/.gradle/daemon (hidden) folder.

You will see each built gradle version folder like 3.2 3.3 4.0.1 4.1 4.10.1 4.10.2 4.4 4.6 5.4.1 5.5

Go into these folders and remove the file named

registry.bin

registry.bin.lock

For me, I remove all these folders.

Then rebuild you app. Should be work now.

Community
  • 1
  • 1
Kyo Kurosagi
  • 1,495
  • 13
  • 14
3

When JVM/Dalvik spends more than 98% doing GC and only 2% or less of the heap size is recovered the “java.lang.OutOfMemoryError: GC overhead limit exceeded” is thrown.

The solution is to extend heap space or use profiling tools/memory dump analyzers and try to find the cause of the problem.

greg8188
  • 56
  • 1
2

My test were failing in gradle version 5.2.1 "GC overhead limit exceeded java.lang.OutOfMemoryError: GC overhead limit exceeded". I solved by adding maxHeapSize in build.gradle like below:

 test {
        maxHeapSize = "4g"
    }
Md Ayub Ali Sarker
  • 7,364
  • 2
  • 19
  • 18
  • I had this issue using when running a test that loaded an Excel file with Apache POI. Setting the `maxHeapSize` to 1g in the `test` section of my `build.gradle` file resolved the issue. – chutch1122 Apr 23 '20 at 14:52
-1

just go to prefrence or settings -> memori settings -> and setup your alocated memory

android studio 3.5

Yudi karma
  • 276
  • 3
  • 13