【问题标题】:Out of memory error: Java heap memory on Android Studio内存不足错误:Android Studio 上的 Java 堆内存
【发布时间】:2015-02-10 20:09:07
【问题描述】:

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

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

我用谷歌搜索了somefix,但找不到合适的。大多数修复都是针对 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:

this 也进行了修复,但也无能为力:

尝试以下方法:

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

VM Options字段写入:

-Xmx2048m -XX:MaxPermSize=512m

【问题讨论】:

  • 检查堆转储...你也可以添加 android:largeHeap="true"
  • @Prachi 没有任何帮助。我什至无法编译和运行它,我想如何检查堆转储?
  • 已编辑。添加了我在 gradlew 文件中所做的修复。
  • 我试过这个Solution,它对我有用。
  • 如果有人遇到此问题并且在 Mac 上使用 AS 3.5+,您可以尝试this GitHub 解决方案。在关于 SO 的众多不同答案中,这对我有用(以及许多其他人的评论反应)。

标签: java android android-studio


【解决方案1】:

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

【讨论】:

  • 同样的错误 :( 我应该向 maxperm 添加更多内存吗?
  • 是的,这样做,看看会发生什么 :) 顺便说一句:你的机器上有多少 RAM?
  • 这是不正确的,XX:MaxPermSize是用来存储“虚拟机描述对象所需的数据,这些对象在Java语言级别不具备等价性。例如描述类和方法的对象是存储在永久代中。” (oracle.com/technetwork/java/javase/gc-tuning-6-140523.html) 我几乎想不出任何场景,AimanB 的代码和 Android Studio 需要超过 512MB 的 RAM 来存储类(not 对象)和方法。问题出在其他地方
  • 你说得有道理。也许现在最好的办法是用 jvisualVM 创建一个转储(它在你 jdk 的 bin 文件夹中),然后用 MAT 分析它,它是免费的,更适合分析转储。
  • 在你启动模拟器之前启动jvisualVM,你可以在jvisualVM的窗口左侧看到活动进程,然后启动模拟器,你会在JVM中看到模拟器的进程并打开监控选项卡,您将看到将为该进程分配多少堆。在您的 java android 进程崩溃后,使用 JVM 创建一个转储,然后使用 MAT 打开该转储并调查所有泄漏。
【解决方案2】:

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

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

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

更新

更多信息android guides

【讨论】:

    【解决方案3】:

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

    dexOptions {
        javaMaxHeapSize "2g"
    }
    

    【讨论】:

    • 格雷姆谢谢! 'gradle.properties' 中的参数由于未知原因总是被忽略。这很有帮助!
    • 如果从命令行使用dx,你需要传递标志-JXmx2gdx -JXmx2g --dex ...
    • 修改为“4g”,内存不足的错误时有发生。
    【解决方案4】:

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

    【讨论】:

    • 我在 File -> other Settings -> Default Settings -> Build, Execution, Deployment -> Compiler -> Android Compilers 中找到了这个配置
    【解决方案5】:

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

    org.gradle.jvmargs=-Xmx1024m

    【讨论】:

    • 重申一下,这需要放在gradle.properties 文件中,而不是build.gradle
    【解决方案6】:

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

    1. Win + X,选择系统
    2. 选择“高级系统设置”
    3. 选择“环境变量”
    4. 创建/编辑名为“_JAVA_OPTIONS”的变量
    5. 将变量值更新为 -Xmx1024m

    click to see snapshot

    【讨论】:

    • 解决了我的问题。 bundletool 报告内存错误 +1。
    【解决方案7】:

    我在 Android Studio 2.3.3 上遇到了类似的问题。当从 Android Studio 构建并尝试处理 300MB .so 库时,APK 打包会失败说 Java heap space。在我的情况下,需要两件事来解决它。您的 build.gradle 应该包含类似的内容:

    android {
        dexOptions {
            javaMaxHeapSize "4G"
        }
    }
    

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

    【讨论】:

    • 重要的是不要错过答案的第二部分;)
    【解决方案8】:

    构建 => 清理项目

    然后

    构建 => 重建项目

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

    【讨论】:

    • 我在很多问题中都看到了这个建议,而且重建确实可以解决大部分问题。困扰我的是为什么我们必须先清洁?重建不是自动清理吗?
    【解决方案9】:

    这就是我所做的:

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

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

    //添加这一行:

    dexOptions {
       javaMaxHeapSize = "4G"
       }
    }
    

    【讨论】:

      【解决方案10】:

      使用 Proguard 的调试版本也可能会出现此问题。要解决它,请将 minifyEnabledshrinkResources 更改为 false。

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

      【讨论】:

        【解决方案11】:

        只需将其添加到 gradle 文件中:

        android{
        testOptions {
                ....
                unitTests.all {
                    jvmArgs "-Xmx2g" // to avoid outOfMemory exception
                }
                ....
            }
        }
        

        【讨论】:

          【解决方案12】:

          没有一个答案对我有用,但最后我发现了一些对我有用的东西:

          自动管理分页文件大小

          在窗口中:

          1. 在“高级系统设置”下
          2. 高级选项卡
          3. 性能部分,设置...
          4. 高级选项卡
          5. 虚拟内存...更改...
          6. 选择自动管理所有驱动器的分页文件大小

          我不记得为什么我没有选中它,但这在我的 gradle 构建中造成了麻烦(有颤动)。

          【讨论】:

            【解决方案13】:

            在项目的根目录中打开 gradle.properties 文件

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

            是的!只需删除行前的# 哈哈哈哈!

            【讨论】:

              【解决方案14】:

              您可以使用 Android Studio 中的 UI 来完成此操作,with friendly defaults

              【讨论】:

                【解决方案15】:

                关闭模拟器,然后编译。默认不允许我同时使用两者

                【讨论】:

                  【解决方案16】:

                  只需将其添加到您的 gradle.properties 文件中,即可避免此错误。

                  # Project-wide Gradle settings.
                  # IDE (e.g. Android Studio) users:
                  # Gradle settings configured through the IDE *will override*
                  # any settings specified in this file.
                  # For more details on how to configure your build environment visit
                  # http://www.gradle.org/docs/current/userguide/build_environment.html
                  # Specifies the JVM arguments used for the daemon process.
                  # The setting is particularly useful for tweaking memory settings.
                  org.gradle.jvmargs=-XX:+UseParallelGC -Xmx2048M -Dkotlin.daemon.jvm.options\="-Xmx4608m" -XX\:MaxPermSize\=1024m -XX\:+HeapDumpOnOutOfMemoryError -Dfile.encoding\=UTF-8
                  # https://developer.android.com/topic/libraries/support-library/androidx-rn
                  android.useAndroidX=true
                  # Kotlin code style for this project: "official" or "obsolete":
                  kotlin.code.style=official
                  # Automatically convert third-party libraries to use AndroidX
                  android.enableJetifier=true
                  #avoid check for debug
                  gradle=build -x lint -x lintVitalRelease
                  #decreases the startup & execution time
                  org.gradle.deamon=true
                  #build project in parallel (milti-module)
                  org.gradle.parallel=true
                  #store task outputs from previous builds
                  org.gradle.caching=true
                  #only required projects changes
                  org.gradle.configureondemand=true
                  

                  但仔细研究每一行。

                  【讨论】:

                    【解决方案17】:

                    经过大量研究,我找到了这个解决方案,我也遇到了同样的问题。 你可以通过改变这个来解决这个问题。转到 gradle.properties 并更改

                    org.gradle.jvmargs=-Xmx500m

                    org.gradle.jvmargs=-Xmx2048m

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 2020-01-23
                      • 2014-01-04
                      • 1970-01-01
                      • 1970-01-01
                      • 2019-07-04
                      • 1970-01-01
                      • 2013-09-10
                      • 2013-05-13
                      相关资源
                      最近更新 更多