简体   繁体   中英

Android Studio gradle build OutOfMemoryError

After a couple of builds that runs between 1-2 minutes Gradle just keeps running forever (have tried to wait for 30 minutes) and the only solution I have is to force close Android Studio and restart. Then I can build again, but after some time, Gradle gets stuck again. And then I can repeat the flow. When I open Activity Monitor it looks like this: 在此处输入图片说明

And here are some of my gradle settings:

compileSdkVersion 26
buildToolsVersion "26.0.1" 
dexOptions {
    preDexLibraries true
    javaMaxHeapSize "4G"
}
productFlavors {
    production {
        dimension "prod"
    }
    development {
        minSdkVersion 21
        resConfigs("en", "xxhdpi")
        aaptOptions {
            cruncherEnabled = false
        }

        ext.enableCrashlytics = false
        dimension "prod"
    }
}

gradle.properties:

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

And my gradle setup

buildscript {
repositories {
    jcenter()
    maven { url 'https://maven.google.com' }
}
dependencies {

    classpath 'com.android.tools.build:gradle:3.0.0-beta5'
    classpath 'com.google.gms:google-services:3.0.0'

}
}

after 25 minutes I get this message (Error:java.lang.OutOfMemoryError: Java heap space): 在此处输入图片说明

What can I do to solve this?

Try to replace in gradlew:

GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""

by

GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\" \"-Xmx1024m\" \"-Xms256m\" \"-XX:MaxPermSize=1024m\""

Using jvmargs your are specifying memory for the task or JVM only. But for your error you should provide memory to Gradle for your case which should be done by specifying in GRADLE_OPTS environment variable.

jvmargs=-Xmx2048m -XX:MaxPermSize=512m

is totally different from

GRADLE_OPTS="$GRADLE_OPTS \"-Xmx1024m\" \"-Xms256m\" \"-XX:MaxPermSize=1024m\""

You can find your project's gradlew in Project View on left pane inside gradle directory

Source : https://groups.google.com/forum/#!msg/adt-dev/BY3GEIVGumU/ogYYDaWf-8gJ

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM