Gradle groovy compile and out of memory error

Today was my first real day with Gradle.

I struggled for hours trying to get the groovy compile process to have enough memory to compile my code and not fall over with out of memory exceptions. This is what I would usually get.

Started Gradle compiler daemon with fork options DaemonForkOptions{minHeapSize=null, maxHeapSize=null, jvmArgs=[], classpath=[/Users/muz/.gvm/grails/1.3.9/lib/groovy-all-1.7.8.jar, /Users/muz/.gvm/gradle/1.8/lib/ant-1.9.2.jar, /Users/muz/.gvm/gradle/1.8/lib/ant-launcher-1.9.2.jar]}.

Executing org.gradle.api.internal.tasks.compile.ApiGroovyCompiler@e4865ce in compiler daemon.

Exception executing org.gradle.api.internal.tasks.compile.ApiGroovyCompiler@e4865ce in compiler daemon: java.lang.OutOfMemoryError: Java heap space.

Things that did not work

Here is a list of things I had tried to get the groovy process to have more heap space.

.bashrc
GRADLE_OPTS=”-Xmx1g”
JAVA_OPTS=”-Xmx1g”

gradle.properties
org.gradle.jvmargs=-Xmx1g

Using Gradlew instead of gradle
DEFAULT_JVM_OPTS=”-Xmx1g”

None of these things would start up the groovy process with the necessary heap to do the compilation.

What worked

The documentation for the scala plugin got me thinking that groovy might also be able to pass in options

tasks.withType(ScalaCompile) {
    configure(scalaCompileOptions.forkOptions) {
        memoryMaximumSize = '1g'
        jvmArgs = ['-XX:MaxPermSize=512m']
    }
}

Scala Plugin with options

With a little editing I managed to get this so all groovy tasks now get a decent amount of heap.

tasks.withType(GroovyCompile) {
  configure(groovyOptions.forkOptions) {
    memoryMaximumSize = '1g'
    jvmArgs = ['-XX:MaxPermSize=512m', '-Xms512m', '-Xmx1g']
  }
}

You can target a single task by setting the needed jvm params like so

compileTestGroovy {
  configure(groovyOptions.forkOptions) {
    memoryMaximumSize = '1g'
    jvmArgs = ['-XX:MaxPermSize=512m', '-Xms512m', '-Xmx1g']
  }
}

With the above you can now pass in useful arguments to the daemon process

Started Gradle compiler daemon with fork options DaemonForkOptions{minHeapSize=null, maxHeapSize=1024m, jvmArgs=[-XX:MaxPermSize=512m, -Xms512m, -Xmx1g], classpath=[~/.gvm/grails/1.3.9/lib/groovy-all-1.7.8.jar, ~/.gvm/gradle/1.8/lib/ant-1.9.2.jar, ~/.gvm/gradle/1.8/lib/ant-launcher-1.9.2.jar]}.

Advertisements

2 thoughts on “Gradle groovy compile and out of memory error

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s