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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27438145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 04:23:04  来源:igfitidea点击:

Out of memory error: Java heap memory on Android Studio

javaandroidandroid-studio

提问by emen

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

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

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.

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

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

我在谷歌上搜索了一些修复,但找不到一个有效的。大多数修复都是针对 Eclipse IDE 的。

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

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

warning: Ignoring InnerClasses attribute for an anonymous inner class
(net.lingala.zip4j.unzip.Unzip) 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) 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) 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) 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) 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.access0(Main.java:82)
    at com.android.dx.command.dexer.Main.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.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) 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) 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) 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) 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) 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.access0(Main.java:82)
        at com.android.dx.command.dexer.Main.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?

是不是因为我使用了一个 jar 库(zip4j)?我确实尝试将其删除,但仍然出现 OOM 错误。知道如何解决这个问题吗?

Edit:

编辑:

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

我还编辑了 gradlew 文件以增加 -Xmx 大小。

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:

编辑2:

Also did thisfix but couldn't help it too:

也做了这个修复,但也无能为力:

Try the following:

请尝试以下操作:

Ctrl + Alt + S -> Compiler -> Gradle

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

InVM Optionsfield write:

VM Options现场写:

-Xmx2048m -XX:MaxPermSize=512m

-Xmx2048m -XX:MaxPermSize=512m

采纳答案by emen

I've finally solved it by limiting the Google Play service in gradle dependency.

我终于通过在 gradle 依赖中限制 Google Play 服务来解决它。

The problem however was caused by the Google Play service, and by simply change it to a specific library, it is now solved.

然而,问题是由 Google Play 服务引起的,只需将其更改为特定的库,现在就解决了。

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

UPDATE

更新

More info on android guides

有关android 指南的更多信息

回答by aurelius

you have changed the wrong arguments, you should increase the size on -XX:MaxPermSize=512M to -XX:MaxPermSize=2048M or so :) because the android emulator uses a lot of RAM.

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

回答by Graeme

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

这是旧的,但只需将其添加到构建文件中

dexOptions {
    javaMaxHeapSize "2g"
}

回答by chef

The answers above are for gradle builds (or the general -Xmx). In case someone gets here for a non-gradle project, I solved the issue by increasing the maximum heap size of DEX in android studio settings => Build, Execution, Deployment => Compiler => Android Compilers

上面的答案适用于 gradle 构建(或一般的 -Xmx)。如果有人来这里进行非 gradle 项目,我通过在 android studio 设置 => 构建、执行、部署 => 编译器 => Android 编译器中增加 DEX 的最大堆大小来解决该问题

回答by HongGang Jing

assign more memory to Gradle in the project's gradle.properties file.For example:

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

org.gradle.jvmargs=-Xmx1024m

org.gradle.jvmargs=-Xmx1024m

回答by Weng Hong Wong

i'm using Windows OS. I solved this by updating _JAVA_OPTIONS to '-Xmx1024m'

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

  1. Win + X, choose SYSTEM
  2. Choose 'Advanced system settings'
  3. Choose 'Environment Variables'
  4. Create / Edit a variable named as '_JAVA_OPTIONS'
  5. update variable value as -Xmx1024m
  1. Win + X,选择系统
  2. 选择“高级系统设置”
  3. 选择“环境变量”
  4. 创建/编辑名为“_JAVA_OPTIONS”的变量
  5. 将变量值更新为 -Xmx1024m

click to see snapshot

点击查看快照

回答by Slion

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

我在 Android Studio 2.3.3 上遇到了类似的问题。Java heap space从 Android Studio 构建并尝试处理 300MB .so 库时,APK 打包会失败。在我的情况下,需要做两件事来修复它。你build.gradle应该包含这样的东西:

android {
    dexOptions {
        javaMaxHeapSize "4G"
    }
}

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

在项目的根目录下,您需要有一个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

回答by Ferit Kartal

Build => Clean Project

构建 => 清理项目

then

然后

Build => Rebuild Project

构建 => 重建项目

When I do this, it always fixes for me.

当我这样做时,它总是为我解决。

回答by Rakesh Deshpande

Here is what I did:

这是我所做的:

1st step: In your App, go to GRADLE SCRIPTS -> GRADLE.PROPERTIES -> change org.gradle.jvmargs=-Xmxmto org.gradle.jvmargs=-Xmx4G

第一步:在您的应用程序中,转到 GRADLE SCRIPTS -> GRADLE.PROPERTIES -> 将org.gradle.jvmargs=-Xmxm更改为org.gradle.jvmargs=-Xmx4G

2nd Step: In your App, go to GRADLE SCRIPTS -> build.gradle -> in the android{

第二步:在你的应用程序中,转到 GRADLE SCRIPTS -> build.gradle -> 在 android{

//add this line:

//添加这一行:

dexOptions {
   javaMaxHeapSize = "4G"
   }
}

回答by twenk11k

This issue also may happen with usage of the debug build of Proguard. To solve it change minifyEnabledand shrinkResourcesto false.

使用 Proguard 的调试版本也可能发生此问题。解决它改变minifyEnabledshrinkResources错误。

android {
...
buildTypes {
    release {
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android.txt')
    }

    debug {
        minifyEnabled false
        shrinkResources false
        proguardFiles getDefaultProguardFile('proguard-android.txt')
    }
} }