Romain Piel
Romain Piel

Reputation: 11177

OutOfMemoryError when compiling my android app with gradle

I'm trying to build my app via gradle and I'm currently having this issue after running a ./gradlew build:

:myApp:compileDebug


The system is out of resources.
Consult the following stack trace for details.
java.lang.OutOfMemoryError: Java heap space
    at com.sun.tools.javac.util.Position$LineMapImpl.build(Position.java:139)
    at com.sun.tools.javac.util.Position.makeLineMap(Position.java:63)
    at com.sun.tools.javadoc.DocCommentScanner.getLineMap(DocCommentScanner.java:438)
    at com.sun.tools.javac.main.JavaCompiler.parse(JavaCompiler.java:512)
    at com.sun.tools.javac.main.JavaCompiler.parse(JavaCompiler.java:550)
    at com.sun.tools.javac.main.JavaCompiler.parseFiles(JavaCompiler.java:804)
    at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:727)
    at com.sun.tools.javac.main.Main.compile(Main.java:353)
    at com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:115)
    at org.gradle.api.internal.tasks.compile.jdk6.Jdk6JavaCompiler.execute(Jdk6JavaCompiler.java:40)
    at org.gradle.api.internal.tasks.compile.jdk6.Jdk6JavaCompiler.execute(Jdk6JavaCompiler.java:33)
    at org.gradle.api.internal.tasks.compile.NormalizingJavaCompiler.delegateAndHandleErrors(NormalizingJavaCompiler.java:95)
    at org.gradle.api.internal.tasks.compile.NormalizingJavaCompiler.execute(NormalizingJavaCompiler.java:48)
    at org.gradle.api.internal.tasks.compile.NormalizingJavaCompiler.execute(NormalizingJavaCompiler.java:34)
    at org.gradle.api.internal.tasks.compile.DelegatingJavaCompiler.execute(DelegatingJavaCompiler.java:29)
    at org.gradle.api.internal.tasks.compile.DelegatingJavaCompiler.execute(DelegatingJavaCompiler.java:20)
    at org.gradle.api.internal.tasks.compile.IncrementalJavaCompilerSupport.execute(IncrementalJavaCompilerSupport.java:33)
    at org.gradle.api.internal.tasks.compile.IncrementalJavaCompilerSupport.execute(IncrementalJavaCompilerSupport.java:24)
    at org.gradle.api.tasks.compile.Compile.compile(Compile.java:68)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1047)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:877)
    at org.gradle.api.internal.BeanDynamicObject$MetaClassAdapter.invokeMethod(BeanDynamicObject.java:216)
    at org.gradle.api.internal.BeanDynamicObject.invokeMethod(BeanDynamicObject.java:122)
    at org.gradle.api.internal.CompositeDynamicObject.invokeMethod(CompositeDynamicObject.java:147)
    at org.gradle.api.tasks.compile.JavaCompile_Decorated.invokeMethod(Unknown Source)
    at groovy.lang.GroovyObject$invokeMethod.call(Unknown Source)
:myApp:compileDebug FAILED

Any idea?

Upvotes: 39

Views: 40989

Answers (9)

Hooppitt
Hooppitt

Reputation: 31

Ok I had this issue as well with a springboot project. I ended up by running the clean task and the problem was solved.

Upvotes: 0

Dr-Bracket
Dr-Bracket

Reputation: 5494

For anyone using Gradle and Kotlin, nothing here worked and I spent hours looking for a solution. Here's what worked for me:

Add this anywhere to your build.gradle:

compileKotlin {
    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_1_8
        freeCompilerArgs = ['-Xjsr305=strict', '-XXLanguage:-NewInference']
    }
}

Upvotes: 0

dfrankow
dfrankow

Reputation: 21387

In my project there was a gradle.properties file with these lines:

# 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

I uncommented the last line, and that worked.

Upvotes: 37

Sanjay Bharwani
Sanjay Bharwani

Reputation: 4759

I also faced this problem on regular java project. Our test execution was extensive and used to run out of memory or permgen error.

So there are two soluton 1. Set the parameters and the run the build

export JAVA_OPTS="-Xmx1024M -XX:MaxPermSize=512M -XX:ReservedCodeCacheSize=512M" 
export GRADLE_OPTS="-Dorg.gradle.daemon=true"

Second option simplifies the solution within in gradle file

test {
    jvmArgs "-XX:MaxPermSize=256m"
}

I recommend second option as its permanent fix.

Upvotes: 7

Ilya
Ilya

Reputation: 11

Changing the JAVA_HOME folder to the 64-bit installation helped me too. Use 64-bit runner for IDEA after that.

Upvotes: 1

rajkabbur
rajkabbur

Reputation: 217

dexOptions {

        incremental true

       //javaMaxHeapSize=1024M for 32bit Java,2048M for 64bit Java

       javaMaxHeapSize "1024M"

       //javaMaxHeapSize "2048M"
    }

Upvotes: 1

denispyr
denispyr

Reputation: 1443

FWIW, I reproduced the "The system is out of resources" error (but with another stacktrace) by hardcoding a ridiculously large string into a source file.

Upvotes: 1

MBulava
MBulava

Reputation: 521

I was having this same issue on my build server, once I changed the JAVA_HOME folder to the 64-bit installation of Java the error went away.

Upvotes: 0

Romain Piel
Romain Piel

Reputation: 11177

I think I fixed it. I got the solution from this post. i.e:

replacing 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\""

Upvotes: 32

Related Questions