【问题标题】:instant run java.lang.OutOfMemoryError: GC overhead limit exceeded即时运行 java.lang.OutOfMemoryError:超出 GC 开销限制
【发布时间】:2016-08-07 04:30:28
【问题描述】:

我已升级到 Android Studio 2.1,但在尝试构建和运行我的公司大项目时出现此错误:

任务“:app:transformClassesWithDexForMyAppDebug”执行失败。 com.android.build.api.transform.TransformException:com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException:java.lang.OutOfMemoryError: 超出 GC 开销限制

我已经搜索了论坛并禁用了即时运行,也写到我的 build.gradle:

dexOptions {
    incremental true
    javaMaxHeapSize "6g"
}
...
dependencies{
    compile 'com.android.support:multidex:'
}

但这并没有解决我的问题。 我在我的 gradle 中启用了 multidex,因为没有它我会收到错误:

com.android.dex.DexIndexOverflowException: 方法 ID 不在 [0, 0xffff] 中:65536

所以这就是它的解决方案,它以前与 Android Studio 的早期版本一起工作(也为公司中使用 Android Studio 1.4-2.0 的其他人工作)但不适合我,因为我升级了我的 Android工作室。

有谁知道是什么导致了这个问题?

有趣的是,如果我只是创建项目,我不会得到错误,只有当我尝试运行它时。任何想法表示赞赏!

编辑1:

还有什么有趣的,如果我重新启动我的 android studio,第一次运行成功,但第二次没有。

编辑2:

如果我将堆大小设置为大于应用程序在第一次运行时甚至无法编译的大小(例如 8-10g)。

编辑 3:

似乎问题出在 instant run,如果我强制 android studio 不使用它(例如一次部署到两个设备或更改 gradle.properties 就像答案中一样),错误就会消失.

【问题讨论】:

  • Instant run 将方法添加到程序中,显然这会使您超过 65k 限制。您是否偶然将播放服务添加到您的依赖项中?
  • 我在设置中禁用了即时运行,例如:stackoverflow.com/a/36637692/3162918
  • 我整天都在试图解决这个错误......
  • 有什么解决办法吗? @bendaf
  • 如果我有我就不会问了:,˙(

标签: android android-studio android-build android-gradle-2.0 android-instant-run


【解决方案1】:

为特定作业设置堆大小的另一种方法是为每个作业使用环境变量。这样可以确保在不使用需要更高内存的作业时内存可用。

GRADLE_OPTS="-Dorg.gradle.jvmargs=-Xms1024M -Xmx8192M -XX:PermSize=512M -XX:MaxPermSize=2048 -XX:+CMSClassUnloadingEnabled -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8"

JAVA_OPTS="-XX:MaxPermSize=2048M"

【讨论】:

    【解决方案2】:
    defaultConfig {
        multiDexEnabled =true
    }
    

    将此添加到应用的 build.gradle 文件中

    【讨论】:

    • 解释答案,让其他人能理解,而不是只问问题。
    • 我已经启用了 multidex,正如我在问题“我在我的 gradle 中启用了 multidex”中所写...
    【解决方案3】:

    Step1:更改 build.grade

    defaultConfig {
            ...
            // Enabling multidex support.
            multiDexEnabled true
           }
    
    dependencies {
            ...
            compile 'com.android.support:multidex:1.0.0'
        }
    

    Step2:在Application类上设置

    public class MyApplication extends Application {
    
    @Override
    public void onCreate() {
        super.onCreate();
        MultiDex.install(this);
    }
    
    }
    

    Step3:更改grade.properties

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

    它会工作的!谢谢。

    【讨论】:

      【解决方案4】:

      将此添加到您的 gradle.properties 文件中。

      # The Gradle daemon aims to improve the startup and execution time of Gradle.
      # When set to true the Gradle daemon is to run the build.
      org.gradle.daemon=true
      
      # Specifies the JVM arguments used for the daemon process.
      # The setting is particularly useful for tweaking memory settings.
      # Default value: -Xmx10248m -XX:MaxPermSize=256m
      org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
      
      # When configured, Gradle will run in incubating parallel mode.
      # This option should only be used with decoupled projects. More details, visit
      # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
      org.gradle.parallel=true
      
      # Enables new incubating mode that makes Gradle selective when configuring projects.
      # Only relevant projects are configured which results in faster builds for large multi-projects.
      # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:configuration_on_demand
      org.gradle.configureondemand=true
      

      找到Here

      在我的build.gradle 上:

      ....
           dexOptions
               {
                     incremental false
                     javaMaxHeapSize "2048M" 
                     preDexLibraries = false
               }//end dexOptions
      
      ....
      

      【讨论】:

      • 看来问题出在即时运行上。据我了解,此设置已禁用。
      • @bendaf 我读过它,但不是因为我的问题,因为我被禁用了即时运行
      • 所以你在编辑你的 gradle.properties 后有即时运行功能吗?你的跑步图标旁边有照明图标吗?因为我在设置中启用了即时运行,但我没有。 ://
      • 是的,我现在已经启用了我编辑的 gradle.properties file ,是的,当我运行应用程序时,照明图标会自动出现在我的运行图标上......也许你的 Android Studio 它已损坏?
      • 今天也遇到了这个问题,这些更改已解决,谢谢!
      猜你喜欢
      • 2017-12-27
      • 1970-01-01
      • 2020-09-08
      • 2020-07-24
      • 1970-01-01
      • 2019-06-29
      • 2015-04-25
      • 1970-01-01
      相关资源
      最近更新 更多