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'
}
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
).
If using the Gradle Wrapper you can set DEFAULT_JVM_OPTS
in gradlew
like this:
DEFAULT_JVM_OPTS="-Xmx512m"
Set it in a similar fashion in gradlew.bat
if you're on Windows:
set DEFAULT_JVM_OPTS=-Xmx512m
The Gradle Wrapper task can also be modified to include this automatically. This is how the Gradle developers have solved it:
wrapper {
gradleVersion = '1.8'
def jvmOpts = "-Xmx512m"
inputs.property("jvmOpts", jvmOpts)
doLast {
def optsEnvVar = "DEFAULT_JVM_OPTS"
scriptFile.write scriptFile.text.replace("$optsEnvVar=\"\"", "$optsEnvVar=\"$jvmOpts\"")
batchScript.write batchScript.text.replace("set $optsEnvVar=", "set $optsEnvVar=$jvmOpts")
}
}
I just found a very nice way to handle this problem. No need for custom gradle wrapper or GRADLE_OPTIONS.
compileJava {
options.fork = true // Fork your compilation into a child process
options.forkOptions.setMemoryMaximumSize("4g") // Set maximum memory to 4g
}
Run Gradle with the --info option to see where it's going to use your parameter for max memory size.
gradle build --info
In the gradle.properties file add the following line :
org.gradle.daemon=true
This will boost the build - taken from
https://www.timroes.de/2013/09/12/speed-up-gradle/
I am using following version of gradle.properties to make Gradle performance better in Android projects
# The Gradle daemon aims to improve the startup and execution time of Gradle.
# When set to true the Gradle daemon is to run the build.
org.gradle.daemon=true
# 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
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
org.gradle.parallel=true
# Enables new incubating mode that makes Gradle selective when configuring projects.
# Only relevant projects are configured which results in faster builds for large multi-projects.
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:configuration_on_demand
org.gradle.configureondemand=true
Put the following content to ~/.gradle as gradle.properties
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Settings specified in this file will override any Gradle settings
# configured through the IDE.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# The Gradle daemon aims to improve the startup and execution time of Gradle.
# When set to true the Gradle daemon is to run the build.
# TODO: disable daemon on CI, since builds should be clean and reliable on servers
org.gradle.daemon=true
# 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=-Xmx6g -Xms4g -XX:MaxPermSize=8g -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
org.gradle.parallel=true
# Enables new incubating mode that makes Gradle selective when configuring projects.
# Only relevant projects are configured which results in faster builds for large multi-projects.
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:configuration_on_demand
org.gradle.configureondemand=true
I personally went through all the articles here but doing these steps fixed it.
If you are using 32 bit jvm that may be the issue install a 64 bit jvm.
- Go to the control panel (search java in windows 10)
- find the java application
- Double click on java and then view.
In run time parameters add:
-Xmx 2048m
None of the above answers worked for me, then I found this-
My System Configuration-
Windows x64 - JDK 32 bit - Cordova 5.4.1 - Ionic 1.7.12
JVM options for running Gradle can be set via environment variables. You can use either GRADLE_OPTS or JAVA_OPTS, or both. JAVA_OPTS is by convention an environment variable shared by many Java applications. A typical use case would be to set the HTTP proxy in JAVA_OPTS and the memory options in GRADLE_OPTS. Those variables can also be set at the beginning of the gradle or gradlew script.
I added the below mentioned two Environment Variables and solved this issue-
Variable Name: GRADLE_OPTS
Variable Value: -Xmx512m
Variable Name: JAVA_OPTS
Variable Value: -Xmx512m
Hope this helps to guys like me.