217

I am using Android Studio on OS X. I am getting this error message:

FAILURE: Build failed with an exception.

  • What went wrong: Execution failed for task ':app:preDexDebug'. com.android.ide.common.internal.LoggedErrorException: Failed to run command: /Applications/Android Studio.app/sdk/build-tools/android-4.4W/dx --dex --output /Users/alex/AndroidStudioProjects/SilentSMS/app/build/intermediates/pre-dexed/debug/android-4.3_r2.1-f22bbff4d1017230e169a4844a9c2195f13060d2.jar /Users/alex/AndroidStudioProjects/SilentSMS/app/libs/android-4.3_r2.1.jar

    Error Code: 3 Output:

  UNEXPECTED TOP-LEVEL ERROR:
  java.lang.OutOfMemoryError: GC overhead limit exceeded
      at com.android.dx.cf.code.RopperMachine.getSources(RopperMachine.java:665)
      at com.android.dx.cf.code.RopperMachine.run(RopperMachine.java:288)
      at com.android.dx.cf.code.Simulator$SimVisitor.visitLocal(Simulator.java:612)
      at com.android.dx.cf.code.BytecodeArray.parseInstruction(BytecodeArray.java:412)
      at com.android.dx.cf.code.Simulator.simulate(Simulator.java:94)
      at com.android.dx.cf.code.Ropper.processBlock(Ropper.java:782)
      at com.android.dx.cf.code.Ropper.doit(Ropper.java:737)
      at com.android.dx.cf.code.Ropper.convert(Ropper.java:346)
      at com.android.dx.dex.cf.CfTranslator.processMethods(CfTranslator.java:282)
      at com.android.dx.dex.cf.CfTranslator.translate0(CfTranslator.java:139)
      at com.android.dx.dex.cf.CfTranslator.translate(CfTranslator.java:94)
      at com.android.dx.command.dexer.Main.processClass(Main.java:682)
      at com.android.dx.command.dexer.Main.processFileBytes(Main.java:634)
      at com.android.dx.command.dexer.Main.access$600(Main.java:78)
      at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:572)
      at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:284)
      at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:166)
      at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:144)
      at com.android.dx.command.dexer.Main.processOne(Main.java:596)
      at com.android.dx.command.dexer.Main.processAllFiles(Main.java:498)
      at com.android.dx.command.dexer.Main.runMonoDex(Main.java:264)
      at com.android.dx.command.dexer.Main.run(Main.java:230)
      at com.android.dx.command.dexer.Main.main(Main.java:199)
      at com.android.dx.command.Main.main(Main.java:103)

I am using this library:

http://grepcode.com/snapshot/repository.grepcode.com/java/ext/com.google.android/android/4.3_r2.1/

I pulled the JAR file and added it to my project - the project I am trying to build is:

https://github.com/domi007/silentSMS/

I understand it is because my xms and xmx values are too low. I increased them in:

/Applications/Android Studio.app/bin/idea.vmoptions so that it now says:

-Xms256m
-Xmx1024m

However, I still get the error. What could this be caused by? Apart from the silentSMS app being an Eclipse project and me porting the code over to Android Studio I haven't changed anything. In terms of Android Studio spotting errors - it doesn't, and everything else looks fine.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
amlwwalker
  • 3,161
  • 4
  • 26
  • 47

13 Answers13

551

I think there's a separate way to raise the heap limit of the dexing operation. Add this to your android closure in your build.gradle file:

dexOptions {
    javaMaxHeapSize "4g"
}

and see if that helps.

(idea courtesy of this answer from Scott Barta)

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 47
    `4g` is a bit overkill, `2g` is ok – Hugo Gresse Sep 14 '15 at 11:58
  • 3
    Fantastic! This also fixed my error. I find that the more memory I give it, the fast the build goes. I went from over 1 min to 19 sec. – Simon Sep 15 '15 at 20:06
  • 1
    After enabling multidex build duration rased from 2 mins up to 6-7 mins. Now it takes the same 2 minutes again! – Dmitry Gryazin Oct 19 '15 at 16:47
  • I am getting this error even after adding this option in Android Studio 2.1 Beta 2 – Vivek Mishra Apr 17 '16 at 17:05
  • Same thing here, something wrong with Android Studio 2.1 Beta 2 – mbonnin Apr 18 '16 at 22:23
  • I'm having the same issue with the latest version of AS (2.1 Beta 2), this might be the cause. – wblaschko Apr 18 '16 at 22:41
  • 2g is enough than 4g – Naveed Ahmad May 13 '16 at 07:48
  • 2
    I still get the error: **java.lang.OutOfMemoryError: GC overhead limit exceeded [org.gradle.api.internal.project.ant.AntLoggingAdapter] at java.util.BitSet.clone** – IgorGanapolsky Aug 17 '16 at 14:57
  • 7
    I have added this, but I still see `OutOfMemoryError` – Chad Bingham Aug 20 '16 at 22:10
  • 1
    Well, if you add javaMaxHeapSize you need to make sure of 2 other things: First, compile 'com.android.support:multidex:1.0.1' in dependencies{ } of build.gradle. And second, android:name="android.support.multidex.MultiDexApplication" in application tag of manifest. – FlorianB Aug 23 '16 at 23:11
  • This is working, thank's ! But is their any backwards of increasing java max heap size? – MHogge Dec 09 '16 at 09:29
  • 1
    @Simon You said: "I find that the more memory I give it, the fast the build goes." I gave it javaMaxHeapSize "4g" and I also noticed that my build was much faster than usual! What are the trade-offs of giving an excessive amount of memory though? – Jaime Montoya Mar 13 '17 at 22:59
  • 1
    Well I guess u have less memory for the rest of your running tasks on your computer during build time but for me, that was not an issue. – Simon Mar 14 '17 at 05:06
  • 9
    didn't work for me, needed to up in gradle.properties: `org.gradle.jvmargs=-XX:MaxPermSize=1024m -Xms1024m -Xmx4096m -XX:ReservedCodeCacheSize=1024m` – hmac Feb 28 '19 at 21:28
  • I put it on 16GB still doesn't owrk – Steve Moretz May 25 '19 at 18:47
  • 1
    In Arctic Fox this produces a warning: "DSL element 'dexOptions' is obsolete and should be removed. It will be removed in version 8.0 of the Android Gradle plugin. Using it has no effect, and the AndroidGradle plugin optimizes dexing automatically." – gmk57 Nov 09 '21 at 14:45
95

In my case, to increase the heap-size looks like this:

Using Android Studio 1.1.0

android {
    dexOptions {
        incremental true
        javaMaxHeapSize "2048M"
    }
}

Put the above code in your Build.gradle file.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dhruv Raval
  • 4,946
  • 3
  • 29
  • 34
  • 4
    this worked to overcome `GC overhead limit exceeded` after I enabled Multidexing. The exception was in `at com.android.dx.command.dexer.Main.runMultiDex(Main.java:334)` – Someone Somewhere Apr 02 '15 at 00:24
  • this isn't going to be pretty as a comment, but the Android Studio error showed ` UNEXPECTED TOP-LEVEL ERROR: java.lang.OutOfMemoryError: GC overhead limit exceeded` The location of exception was at `at com.android.dx.util.FixedSizeList.(FixedSizeList.java:38)` and top of stack was `com.android.dx.command.dexer.Main.runMultiDex(Main.java:334) at com.android.dx.command.dexer.Main.run(Main.java:244) at com.android.dx.command.dexer.Main.main(Main.java:215) at com.android.dx.command.Main.main(Main.java:106)` – Someone Somewhere Apr 02 '15 at 18:07
  • 1
    But as soon as I added you suggested javaMaxHeapSize and incremental parameters, Android Studio was able to build the multi-dexed app. – Someone Somewhere Apr 02 '15 at 18:08
  • if 2gb isn't enough a some point, then I'll try 4gb that commonware suggested. – Someone Somewhere Apr 02 '15 at 18:10
  • 1
    Thanks. Notice what the docs say about `incremental`: *This has many limitations and may not work. Use carefully.* – Ferran Maylinch Sep 15 '15 at 11:42
  • incremental is not tagged as deprecated – MHogge Dec 09 '16 at 09:27
46

This new issue is caused by the latest version of Android.

Go to your project root folder, open gradle.properties, and add the following options:

org.gradle.daemon=true

org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

org.gradle.parallel=true

org.gradle.configureondemand=true

Then add these changes in your build.gradle file:

dexOptions {
        incremental = true
        preDexLibraries = false
        javaMaxHeapSize "4g" // 2g should be also OK
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 2
    Warning:The `android.dexOptions.incremental` property is deprecated and it has no effect on the build process. – tir38 Jun 21 '16 at 20:55
  • this solved my issue. but i;ts creating a file called java_pid1512.hprof which is very large. i am guessing your Dfile option is creating it ? is it necessary and can you show how to remove the dump to file – j2emanue Aug 06 '17 at 07:15
  • 1
    @j2emanue: This file is created because of the "HeapDumpOnOutOfMemoryError" option: https://docs.oracle.com/cd/E15289_01/doc.40/e15062/optionxx.htm#BABDJEJI – Mehlyfication Sep 11 '17 at 08:41
  • 1
    May need to updated: `incremental` is deleted in the end of 2018 – cuasodayleo Jan 09 '19 at 04:22
  • in AS 3.4 enabling the "org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8" in the gradle.properties file solved the issue for me – Jamal S May 08 '19 at 07:43
10

Android Studio 3.5.3

Find the Memory Settings (Cmd + Shift + A on Mac or click on Help and start typing "Memory Settings") under Preferences/ Settings and increase the IDE Heap Size and/ or the Daemon Heap Size to your satisfaction enter image description here

ericn
  • 12,476
  • 16
  • 84
  • 127
4

I disable my Instant Run by:

Menu PreferenceBuildInstant Run "Enable Instant Run to hot swap code"

I guess it is the Instant Run that makes the build slow and creates a large size pidXXX.hprof file which causes the AndroidStudio gc overhead limit exceeded.

(My device SDK is 19.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
崔乐天
  • 41
  • 3
  • How did you find out it was Instant Run that created such a large hprof file? I noticed one created recently on our GitLab server, but we don't have instant run enabled. – AdamMc331 May 15 '19 at 17:07
1

For me non of the answers worked I saw here worked. I guessed that having the CPU work extremely hard makes the computer hot. After I closed programs that consume large amounts of CPU (like chrome) and cooling down my laptop the problem disappeared.

For reference: I had the CPU on 96%-97% and Memory usage over 2,000,000K by a java.exe process (which was actually gradle related process).

Noy Oliel
  • 1,430
  • 3
  • 13
  • 26
1

Add this to build.gradle file

dexOptions {
   javaMaxHeapSize "2g"
}
Ghulam Rasool
  • 3,996
  • 2
  • 27
  • 40
1

4g is a bit overkill, if you do not want to change buildGradle you can use FILE -> Invalid caches / restart.

Thats work fine for me ...

Pabel
  • 652
  • 7
  • 15
0

I forced closed all Java.exe from taskmanger, restarted Android Studio and it worked for me

enter image description here

Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
0

I'm using Android Studio 3.4 and the only thing that worked for me was to remove the following lines from my build.gradle file:

minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

Because Android Studio 3.4 is using R8 in full mode and is not direct compatible with Proguard

pableiros
  • 14,932
  • 12
  • 99
  • 105
  • 2
    I think this isn't a solution when you have to release an obfuscated build. I have used **gradle-5.2.1** with **Android Studio 3.4** and it's working fine even with the `minifyEnabled true` with pro-guard configuration while **gradle-5.1.1** was having similar issues. – Vikas Patidar Apr 23 '19 at 22:12
  • This is the exact opposite of what we want. -1 – Zun Apr 29 '19 at 09:37
  • Like I said, this worked for me and the error is gone and finally I was able to generate the apk. None of the others answers worked in my project. – pableiros Apr 29 '19 at 21:32
  • update gradle to **5.2.1** run cammand in **/project/android** `./gradlew wrapper --gradle-version=5.2.1` – Ashok Devatwal Oct 21 '19 at 17:50
0

in my case, I Edit my gradle.properties :

note: if u enable the minifyEnabled true :

remove this line :

android.enableR8=true

and add this lines in ur build.gradle , android block :

  dexOptions {
        incremental = true
        preDexLibraries = false
        javaMaxHeapSize "4g" // 2g should be also OK
    }

hope this help some one :)

Sana Ebadi
  • 6,656
  • 2
  • 44
  • 44
0

At some point a duplicate copy of apply plugin: 'com.android.application' got added to my build gradle. Removing the duplicate copy and making sure all my apply plugins were at the top fixed the issue for me.

mburst
  • 531
  • 4
  • 7
0

I tried all the solutions mentioned above, but I was still facing the issue. Finally I stumbled upon the following which resolved the issue.

(On MacOS) Went to Preferences -> Memory Settings -> Find existing grade demon(s) -> Stopped all of them.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Cyril Noah
  • 15
  • 1
  • 8