39

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?

2
  • You can also try the --no-daemon gradle arg. If there is a daemon running it won't pick up the changes until it is restarted. Commented Jun 12, 2014 at 22:42
  • For those coming across this error in Android Studio, go to Build -> Clean Project and it solved it for me. Commented Jul 19, 2017 at 0:25

9 Answers 9

37

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Actually I changed last line to: org.gradle.jvmargs=-Xmx10248m -XX:MaxPermSize=2048m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
MaxPermSize support was removed in 8.0! stackoverflow.com/questions/18339707/…
Check out this doc on where gradle.properties file locates and the loading order: docs.gradle.org/current/userguide/…
32

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

5 Comments

This solution worked great for me. This java heap space problem was annoying. IT was not a problem for debug build though.
There is also a DEFAULT_JVM_OPTS variable at the top of the gradlew file where you can put the options. DEFAULT_JVM_OPTS="\"-Xmx1024m\" \"-Xms256m\" \"-XX:MaxPermSize=1024m\""
For windows you have to change gradle.bat instead of gradlew. That also worked for me (giving a Kotlin training and running against this problem.. :-S). Moreover, it modified DEFAULT_JVM_OPTS. Thanks!
this sounds like a hack, it's much better to use the gradle.properties as mentioned in @dfrankow's answer
Probably, this was written 9 years ago 😅
7

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.

2 Comments

MaxPermSize support was removed in 8.0! stackoverflow.com/questions/18339707/…
Yes, my work was on Java7. Things would have changed.
1

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.

Comments

1
dexOptions {

        incremental true

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

       javaMaxHeapSize "1024M"

       //javaMaxHeapSize "2048M"
    }

Comments

1

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

Comments

0

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.

Comments

0

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']
    }
}

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.