title: How to optimize gradle build performance regarding build duration and RAM usage?
body: <p>I am currently switching from ant to gradle for my multi module web application and at the moment it seems that the current version of Gradle (M9) might be running up against its limits. But maybe (hopefully) it is just a problem of me not understanding the concepts of Gradle good enough or not knowing the "magic performance boost switch". I'd be happy for any hint on how the build performance could be optimized.</p>
  
  <p>The problems: several minutes pass before the first <code>compileJava</code> is displayed, and even if nothing has changed in the sources, the process is running at least 7 minutes until it crashes halfway through <code>:testClasses</code> (at varying subprojects) with the following message:</p>
  
  <pre><code>* What went wrong:
  Could not resolve all dependencies for configuration ':mysubproject_X:testRuntime'.
  &gt; Java heap space
  </code></pre>
  
  <p>The project consists of about 30 (partly interdependent) subprojects, the build.gradle of them being more or less the same and are used to build a jar file from each subproject, e.g.</p>
  
  <pre><code>sourceSets {
  
      main {
          java {
              srcDirs 'src'
          }
      }
  }
  
  dependencies {
  
      compile project(':mysubproject_A')
      compile project(':mysubproject_B')
      compile project(':mysubproject_E')
  
      compile group: 'commons-lang', name: 'commons-lang', version: '2.2'
  
  }
  
  // copy all non-java files from src
  copy {
      from sourceSets.main.java.srcDirs
      into "$buildDir/classes/main"
      exclude '**/*.java'
  }
  
  jar {
  }
  </code></pre>
  
  <p>I tried to resolve the heap space problem by ramping up max memory size to 1024M, but it did not help. My main build.gradle file looks like this:</p>
  
  <pre><code>            sourceCompatibility = 1.6
              version = 0.5
  
              useFindBugs = false
  
              apply plugin: 'java'
  
              configurations {
              }
  
              repositories {
                  mavenCentral()
                  mavenRepo url:"http://repository.jboss.org/maven2", artifactUrls: ["https://repository.jboss.org/nexus/content/repositories/public","http://opensource.55minutes.com/maven-releases"]
              }
  
  
              dependencies {
              }
  
              buildscript {
                  repositories {
                      mavenRepo url: 'http://gradle.artifactoryonline.com/gradle/plugins'
                      flatDir(dirs: "$projectDir/lib")
                  }
  
                  dependencies {
                      classpath "org.gradle.plugins:gradle-idea-plugin:0.3.1"
                  }
              }
  
              subprojects {
                  apply plugin: 'java'
                  apply plugin: 'idea'
  
                  repositories {
                      mavenCentral()
                      mavenRepo url:"http://repository.jboss.org/maven2", artifactUrls: ["https://repository.jboss.org/nexus/content/repositories/public","http://opensource.55minutes.com/maven-releases"]
                  }
  
                  dependencies {
                      testCompile 'junit:junit:4.8.2'
                  }
  
                  compileJava {
                      options.encoding = 'UTF-8'
                      options.fork (memoryMaximumSize: '1024m') 
                  }
  
                  javadoc {
                      options.encoding = 'UTF-8'
                  }
  
                  test {
                      testReportDir = file(rootProject.testReportDir)
                      forkEvery = 1
                      jvmArgs = ['-ea', '-Xmx1024m']
                  }
              }
  
  
              dependsOnChildren()
  
              task wrapper(type: Wrapper) {
                  gradleVersion = '1.0-milestone-9'
              }
  </code></pre>