Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Out of memory error: Java heap memory on Android Studio

How do I fix java.lang.OutOfMemoryError: Java heap space when compiling my Android project?

I get this after I upgraded to version 1 of Android Studio. But, I don't think this is the problem. Most likely when I start upgrading my app to SDK 21 (before this was SDK 20). But I wasn't so sure either.

I've googled around for some fix, but couldn't find one that works. Most of the fix are for Eclipse IDE.

This is the full logcat error that I get when compiling:

warning: Ignoring InnerClasses attribute for an anonymous inner class (net.lingala.zip4j.unzip.Unzip$1) that doesn't come with an associated EnclosingMethod attribute. This class was probably produced by a compiler that did not target the modern .class file format. The recommended solution is to recompile the class from source, using an up-to-date compiler and without specifying any "-target" type options. The consequence of ignoring this warning is that reflective operations on this class will incorrectly indicate that it is *not* an inner class. warning: Ignoring InnerClasses attribute for an anonymous inner class (net.lingala.zip4j.unzip.Unzip$2) that doesn't come with an associated EnclosingMethod attribute. This class was probably produced by a compiler that did not target the modern .class file format. The recommended solution is to recompile the class from source, using an up-to-date compiler and without specifying any "-target" type options. The consequence of ignoring this warning is that reflective operations on this class will incorrectly indicate that it is *not* an inner class. warning: Ignoring InnerClasses attribute for an anonymous inner class (net.lingala.zip4j.util.ArchiveMaintainer$1) that doesn't come with an associated EnclosingMethod attribute. This class was probably produced by a compiler that did not target the modern .class file format. The recommended solution is to recompile the class from source, using an up-to-date compiler and without specifying any "-target" type options. The consequence of ignoring this warning is that reflective operations on this class will incorrectly indicate that it is *not* an inner class. warning: Ignoring InnerClasses attribute for an anonymous inner class (net.lingala.zip4j.util.ArchiveMaintainer$2) that doesn't come with an associated EnclosingMethod attribute. This class was probably produced by a compiler that did not target the modern .class file format. The recommended solution is to recompile the class from source, using an up-to-date compiler and without specifying any "-target" type options. The consequence of ignoring this warning is that reflective operations on this class will incorrectly indicate that it is *not* an inner class. warning: Ignoring InnerClasses attribute for an anonymous inner class (net.lingala.zip4j.zip.ZipEngine$1) that doesn't come with an associated EnclosingMethod attribute. This class was probably produced by a compiler that did not target the modern .class file format. The recommended solution is to recompile the class from source, using an up-to-date compiler and without specifying any "-target" type options. The consequence of ignoring this warning is that reflective operations on this class will incorrectly indicate that it is *not* an inner class.  UNEXPECTED TOP-LEVEL ERROR: java.lang.OutOfMemoryError: Java heap space     at com.android.dx.rop.code.RegisterSpec$ForComparison.toRegisterSpec(RegisterSpec.java:642)     at com.android.dx.rop.code.RegisterSpec.intern(RegisterSpec.java:73)     at com.android.dx.rop.code.RegisterSpec.make(RegisterSpec.java:90)     at com.android.dx.cf.code.RopperMachine.run(RopperMachine.java:323)     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:787)     at com.android.dx.cf.code.Ropper.doit(Ropper.java:742)     at com.android.dx.cf.code.Ropper.convert(Ropper.java:349)     at com.android.dx.dex.cf.CfTranslator.processMethods(CfTranslator.java:280)     at com.android.dx.dex.cf.CfTranslator.translate0(CfTranslator.java:137)     at com.android.dx.dex.cf.CfTranslator.translate(CfTranslator.java:93)     at com.android.dx.command.dexer.Main.processClass(Main.java:729)     at com.android.dx.command.dexer.Main.processFileBytes(Main.java:673)     at com.android.dx.command.dexer.Main.access$300(Main.java:82)     at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:602)     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:632)     at com.android.dx.command.dexer.Main.processAllFiles(Main.java:505)     at com.android.dx.command.dexer.Main.runMultiDex(Main.java:332)     at com.android.dx.command.dexer.Main.run(Main.java:243)     at com.android.dx.command.dexer.Main.main(Main.java:214)     at com.android.dx.command.Main.main(Main.java:106)   FAILED  FAILURE: Build failed with an exception.  * What went wrong: Execution failed for task ':app:dexDebug'. > com.android.ide.common.internal.LoggedErrorException: Failed to run command:     C:\Users\piracyde25\android-sdks\build-tools\21.1.1\dx.bat --dex --no-optimize --multi-dex --main-dex-list C:\Users\piracyde25\workspace\Baucar\app\build\intermediates\multi-dex\debug\maindexlist.txt --output C:\Users\piracyde25\workspace\Baucar\app\build\intermediates\dex\debug --input-list=C:\Users\piracyde25\workspace\Baucar\app\build\intermediates\tmp\dex\debug\inputList.txt Error Code:     3 Output:     warning: Ignoring InnerClasses attribute for an anonymous inner class     (net.lingala.zip4j.unzip.Unzip$1) that doesn't come with an     associated EnclosingMethod attribute. This class was probably produced by a     compiler that did not target the modern .class file format. The recommended     solution is to recompile the class from source, using an up-to-date compiler     and without specifying any "-target" type options. The consequence of ignoring     this warning is that reflective operations on this class will incorrectly     indicate that it is *not* an inner class.     warning: Ignoring InnerClasses attribute for an anonymous inner class     (net.lingala.zip4j.unzip.Unzip$2) that doesn't come with an     associated EnclosingMethod attribute. This class was probably produced by a     compiler that did not target the modern .class file format. The recommended     solution is to recompile the class from source, using an up-to-date compiler     and without specifying any "-target" type options. The consequence of ignoring     this warning is that reflective operations on this class will incorrectly     indicate that it is *not* an inner class.     warning: Ignoring InnerClasses attribute for an anonymous inner class     (net.lingala.zip4j.util.ArchiveMaintainer$1) that doesn't come with an     associated EnclosingMethod attribute. This class was probably produced by a     compiler that did not target the modern .class file format. The recommended     solution is to recompile the class from source, using an up-to-date compiler     and without specifying any "-target" type options. The consequence of ignoring     this warning is that reflective operations on this class will incorrectly     indicate that it is *not* an inner class.     warning: Ignoring InnerClasses attribute for an anonymous inner class     (net.lingala.zip4j.util.ArchiveMaintainer$2) that doesn't come with an     associated EnclosingMethod attribute. This class was probably produced by a     compiler that did not target the modern .class file format. The recommended     solution is to recompile the class from source, using an up-to-date compiler     and without specifying any "-target" type options. The consequence of ignoring     this warning is that reflective operations on this class will incorrectly     indicate that it is *not* an inner class.     warning: Ignoring InnerClasses attribute for an anonymous inner class     (net.lingala.zip4j.zip.ZipEngine$1) that doesn't come with an     associated EnclosingMethod attribute. This class was probably produced by a     compiler that did not target the modern .class file format. The recommended     solution is to recompile the class from source, using an up-to-date compiler     and without specifying any "-target" type options. The consequence of ignoring     this warning is that reflective operations on this class will incorrectly     indicate that it is *not* an inner class.      UNEXPECTED TOP-LEVEL ERROR:     java.lang.OutOfMemoryError: Java heap space         at com.android.dx.rop.code.RegisterSpec$ForComparison.toRegisterSpec(RegisterSpec.java:642)         at com.android.dx.rop.code.RegisterSpec.intern(RegisterSpec.java:73)         at com.android.dx.rop.code.RegisterSpec.make(RegisterSpec.java:90)         at com.android.dx.cf.code.RopperMachine.run(RopperMachine.java:323)         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:787)         at com.android.dx.cf.code.Ropper.doit(Ropper.java:742)         at com.android.dx.cf.code.Ropper.convert(Ropper.java:349)         at com.android.dx.dex.cf.CfTranslator.processMethods(CfTranslator.java:280)         at com.android.dx.dex.cf.CfTranslator.translate0(CfTranslator.java:137)         at com.android.dx.dex.cf.CfTranslator.translate(CfTranslator.java:93)         at com.android.dx.command.dexer.Main.processClass(Main.java:729)         at com.android.dx.command.dexer.Main.processFileBytes(Main.java:673)         at com.android.dx.command.dexer.Main.access$300(Main.java:82)         at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:602)         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:632)         at com.android.dx.command.dexer.Main.processAllFiles(Main.java:505)         at com.android.dx.command.dexer.Main.runMultiDex(Main.java:332)         at com.android.dx.command.dexer.Main.run(Main.java:243)         at com.android.dx.command.dexer.Main.main(Main.java:214)         at com.android.dx.command.Main.main(Main.java:106) 

Is it because of one jar library I used (zip4j)? I did try to remove it, but the OOM error still occurs. Any idea how to fix this?

Edit:

I've also edited gradlew file to increase the -Xmx size.

GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\" \"-Xmx2048M \"-XX:MaxPermSize=512M\" \"-XX:ReservedCodeCacheSize=512M\"" 

Nothing changes.

Edit 2:

Also did this fix but couldn't help it too:

Try the following:

Ctrl + Alt + S -> Compiler -> Gradle

InVM Options field write:

-Xmx2048m -XX:MaxPermSize=512m

like image 245
emen Avatar asked Dec 12 '14 06:12

emen


People also ask

How do I fix out of memory error in Android Studio?

If you haven't yet encountered an OOM in your Android application, you will in the near future. Memory leaks cause the OutOfMemoryError in Android. To eliminate the OutOfMemoryError, you must first eliminate memory leaks from your Android application.

How do I fix Java out of memory error heap space?

OutOfMemoryError: Java heap space. 1) An easy way to solve OutOfMemoryError in java is to increase the maximum heap size by using JVM options "-Xmx512M", this will immediately solve your OutOfMemoryError.

How do you fix heap memory problems?

There are several ways to eliminate a heap memory issue: Increase the maximum amount of heap available to the VM using the -Xmx VM argument. Use partitioning to distribute the data over additional machines. Overflow or expire the region data to reduce the heap memory footprint of the regions.

What is maximum heap size in Android Studio?

By default, Android Studio has a maximum heap size of 1280MB. If you are working on a large project, or your system has a lot of RAM, you can improve performance by increasing the maximum heap size for Android Studio processes, such as the core IDE, Gradle daemon, and Kotlin daemon.


2 Answers

I had a similar issue on Android Studio 2.3.3. APK packaging would fail saying Java heap space when building from Android Studio and trying to process a 300MB .so library. In my case two things were needed to fix it. Your build.gradle should contain something like that:

android {     dexOptions {         javaMaxHeapSize "4G"     } } 

At the root of your project you need to have a file named gradle.properties containing the following:

# That's needed for java to be able the create the APK with our 300MB native  library # Basically we grant java a 4GB heap. org.gradle.jvmargs=-Xmx4608m 
like image 139
Slion Avatar answered Sep 21 '22 06:09

Slion


This is old, but just add this to the build file

dexOptions {     javaMaxHeapSize "2g" } 
like image 29
Graeme Avatar answered Sep 17 '22 06:09

Graeme