How to optimize gradle build performance regarding build duration and RAM usage?

peterp picture peterp · Mar 21, 2012 · Viewed 70.4k times · Source

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.

The problems: several minutes pass before the first compileJava is displayed, and even if nothing has changed in the sources, the process is running at least 7 minutes until it crashes halfway through :testClasses (at varying subprojects) with the following message:

* What went wrong:
Could not resolve all dependencies for configuration ':mysubproject_X:testRuntime'.
> Java heap space

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.

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 {
}

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:

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

Answer

Peter Niederwieser picture Peter Niederwieser · Mar 21, 2012

You need to give more memory to the Gradle JVM, not to the compile task/JVM. One way to do so is via the GRADLE_OPTS environment variable (GRADLE_OPTS=-Xmx512m).