Search code examples
scalagradlepermgen

How do I increase PermGen space for Scala compilation under Gradle?


I'm getting org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':compileScala' trying to build my Scala project with Gradle, and if I run Gradle with --stacktrace it looks like the culprit is java.lang.OutOfMemoryError: PermGen space.

I tried forking the compilation task and adding MaxPermSize to the forked JVM arguments, but all that did is move the OOME behind an InvocationTargetException.

Here's my build.gradle block for the compileScala task:

compileScala {
  options.fork = true
  options.forkOptions.jvmArgs = ['-XX:MaxPermSize=512m']
  scalaCompileOptions.additionalParameters = [
      "-feature",
      "-language:reflectiveCalls", // used for config structural typing
      "-language:postfixOps"
  ]
}

I know I can work around this by increasing PermGen space globally using $JAVA_OPTIONS, but I'd prefer a solution that I can check in with the source so that nobody needs to customize anything locally to build the project.

FWIW, I'm using Gradle 1.11 with Scala 2.10.3 under Java 1.6.0_65 on Mac OS 10.9.3.


Solution

  • Found it: Even though the ScalaCompile task has the options property, that property isn't used for Scala compilation; you need to use scalaCompileOptions instead. It in turn has its own forkOptions property.

    compileScala {
        scalaCompileOptions.fork = true
        scalaCompileOptions.forkOptions.jvmArgs = ['-XX:MaxPermSize=512m']
        scalaCompileOptions.additionalParameters = [
            "-feature",
            "-language:reflectiveCalls", // used for config structural typing
            "-language:postfixOps"
        ]
    }