内存不足错误:Android Studio上的Java堆内存

如何修复java.lang.OutOfMemoryError: Java heap space编译Android项目时的java.lang.OutOfMemoryError: Java heap space

升级到Android Studio版本1后,我得到了这个。 但是,我不认为这是问题。 当我开始将我的应用程序升级到SDK 21(在此之前是SDK 20)时,可能性很大。 但是我也不太确定。

我已经search了一些 修复 ,但找不到一个工作。 大多数修复都是针对Eclipse IDE的。

这是编译时得到的完整的logcat错误:

 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) 

是因为我使用了一个jar库(zip4j)吗? 我尝试删除它,但OOM错误仍然发生。 任何想法如何解决这个问题?

编辑:

我也编辑gradlew文件来增加-Xmx大小。

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

没有什么变化。

编辑2:

也做了这个修复,但也无法帮助:

尝试以下操作:

Ctrl + Alt + S – >编译器 – > Gradle

在“ VM Options字段中input:

-Xmx2048m -XX:MaxPermSize = 512m

Solutions Collecting From Web of "内存不足错误:Android Studio上的Java堆内存"

这是旧的,但只是将其添加到构build文件

 dexOptions { javaMaxHeapSize "2g" } 

我终于通过在Gradle依赖中限制Google Play服务来解决这个问题。

但问题是由Google Play服务引起的,只需将其更改为特定的库即可解决问题。

  com.google.android.gms:play-services-base:6.5.87 

UPDATE

有关Android指南的更多信息

上面的答案是用于gradle build(或者-Xmx)。 如果有人来到这里做一个非gradle项目,我通过在android studio设置中增加DEX的最大堆大小来解决这个问题=> Build,Execution,Deployment => Compiler => Android Compilers

你已经改变了错误的参数,你应该增加-XX的大小:MaxPermSize = 512M到-XX:MaxPermSize = 2048M左右:)因为android模拟器使用了大量的RAM。

在项目的gradle.properties文件中为Gradle分配更多内存。例如:

org.gradle.jvmargs = -Xmx1024m

我正在使用Windows操作系统。 我解决了这个问题,将_JAVA_OPTIONS更新为'-Xmx1024m'

  1. Win + X,selectSYSTEM
  2. select“高级系统设置”
  3. select“环境variables”
  4. 创build/编辑名为'_JAVA_OPTIONS'的variables
  5. 更新variables值为-Xmx1024m

点击查看快照

我在Android Studio 2.3.3上遇到过类似的问题。 从Android Studio构build并尝试处理300MB .so库时,APK封装将无法说明Java heap space 。 在我的情况下,需要两件事来解决它。 你的build.gradle应该包含这样的东西:

 android { dexOptions { javaMaxHeapSize "4G" } } 

在项目的根目录下,您需要有一个名为gradle.properties的文件, gradle.properties包含以下内容:

 # 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