instant run java.lang.OutOfMemoryError: GC overhead limit exceeded

instant run java.lang.OutOfMemoryError: GC overhead limit exceeded

我已经升级到 Android Studio 2.1,但我在尝试构建和 运行 我的公司大项目时遇到了这个错误:

Execution failed for task ':app:transformClassesWithDexForMyAppDebug'. com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: java.lang.OutOfMemoryError: GC overhead limit exceeded

我已经通过论坛搜索并禁用即时 运行,也写信给我 build.gradle:

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

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

com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536

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

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

同样有趣的是,如果我只是制作项目,我不会收到错误,只有当我尝试 运行 时才会出现。任何想法表示赞赏!

编辑 1:

有趣的是,如果我重新启动我的 android 工作室,第一个 运行 会成功,但第二个不会。

编辑 2:

如果我将堆大小设置为更大(如 8-10g),应用程序甚至一开始都无法编译 运行。

编辑 3:

似乎问题出在 instant 运行,如果我强制 android studio 不使用它(比如同时部署到两个设备或像答案中那样更改 gradle.properties)错误消失。

将此添加到您的 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

....

第 1 步:更改 build.grade

defaultConfig {
        ...
        // Enabling multidex support.
        multiDexEnabled true
       }

dependencies {
        ...
        compile 'com.android.support:multidex:1.0.0'
    }

第二步:应用程序设置class

public class MyApplication extends Application {

@Override
public void onCreate() {
    super.onCreate();
    MultiDex.install(this);
}

}

第三步:改变grade.properties

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

它会起作用的!谢谢

defaultConfig {
    multiDexEnabled =true
}

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

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

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"