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?

Romain Piel
  • 11,017
  • 15
  • 71
  • 106

9 Answers9

36

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.

dfrankow
  • 20,191
  • 41
  • 152
  • 214
  • Actually I changed last line to: org.gradle.jvmargs=-Xmx10248m -XX:MaxPermSize=2048m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 – Benjamin Jul 30 '20 at 18:12
  • 1
    MaxPermSize support was removed in 8.0! https://stackoverflow.com/questions/18339707/permgen-elimination-in-jdk-8 – Felipe Desiderati Jun 10 '21 at 20:15
  • Check out this doc on where `gradle.properties` file locates and the loading order: https://docs.gradle.org/current/userguide/build_environment.html#sec:gradle_configuration_properties – EasonL Jul 17 '23 at 19:11
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\""
Romain Piel
  • 11,017
  • 15
  • 71
  • 106
  • This solution worked great for me. This java heap space problem was annoying. IT was not a problem for debug build though. – mask Feb 03 '14 at 23:32
  • 10
    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\"" – Cameron Ketcham Mar 24 '14 at 23:14
  • 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! – Bram Janssens Jul 01 '21 at 07:23
  • this sounds like a hack, it's much better to use the gradle.properties as mentioned in @dfrankow's answer – Gavriel May 12 '22 at 11:01
  • Probably, this was written 9 years ago – Romain Piel May 19 '22 at 14:08
6

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.

Sanjay Bharwani
  • 3,317
  • 34
  • 31
1
dexOptions {

        incremental true

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

       javaMaxHeapSize "1024M"

       //javaMaxHeapSize "2048M"
    }
gio
  • 4,950
  • 3
  • 32
  • 46
rajkabbur
  • 187
  • 3
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.

denispyr
  • 1,403
  • 3
  • 21
  • 34
1

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

Ilya
  • 11
  • 1
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.

MBulava
  • 521
  • 3
  • 9
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']
    }
}
Dr-Bracket
  • 4,299
  • 3
  • 20
  • 28
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.

Hooppitt
  • 31
  • 3