如何优化编译时间和RAM使用的gradle编译性能?
我目前正在从ant切换到Gradle for我的多模块Web应用程序,目前看来,当前版本的Gradle(M9)可能会跑到它的极限。 但也许(希望)这只是一个问题,我没有理解Gradle的概念够好或不知道“魔术表演升压开关”。 我很乐意提供有关如何优化构build性能的提示。
问题是:在显示第一个compileJava
之前几分钟,即使源代码中没有任何更改,该进程至less运行7分钟,直到中途崩溃:testClasses
(在不同的子项目中),带有以下消息:
* What went wrong: Could not resolve all dependencies for configuration ':mysubproject_X:testRuntime'. > Java heap space
该项目由大约30个(部分相互依赖的)子项目组成,它们的构build梯度大致相同,并且用于从每个子项目构build一个jar文件,例如
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 { }
我试图通过将最大内存大小增加到1024M来解决堆空间问题,但是没有帮助。 我的主build.gradle文件如下所示:
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' }
你需要给Gradle JVM更多的内存,而不是编译任务/ JVM。 一种方法是通过GRADLE_OPTS
环境variables( GRADLE_OPTS=-Xmx512m
)。
如果使用Gradle Wrapper,你可以像这样在gradlew
设置DEFAULT_JVM_OPTS
:
DEFAULT_JVM_OPTS="-Xmx512m"
如果你使用的是Windows,请在gradlew.bat
中以类似的方式进行设置:
set DEFAULT_JVM_OPTS=-Xmx512m
Gradle Wrapper任务也可以被修改以自动包含这个任务。 这就是Gradle开发人员已经解决的问题:
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") } }
我刚刚find一个很好的方法来处理这个问题。 无需定制gradle包装或GRADLE_OPTIONS。
compileJava { options.fork = true // Fork your compilation into a child process options.forkOptions.setMemoryMaximumSize("4g") // Set maximum memory to 4g }
使用–info选项运行Gradle,查看它将使用您的参数的最大内存大小。
gradle build --info
在gradle.properties文件中添加以下行:
org.gradle.daemon =真
这将促进build设
我正在使用以下版本的gradle.properties在Android项目中使Gradle性能更好
# 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
上面的答案都没有为我工作,然后我发现这个 –
我的系统configuration –
Windows x64 - JDK 32 bit - Cordova 5.4.1 - Ionic 1.7.12
运行Gradle的JVM选项可以通过环境variables来设置。 您可以使用GRADLE_OPTS或JAVA_OPTS,或者两者都使用。 按照惯例,JAVA_OPTS是许多Java应用程序共享的环境variables。 一个典型的用例是在JAVA_OPTS中设置HTTP代理,在GRADLE_OPTS中设置内存选项。 这些variables也可以设置在gradle或gradlew脚本的开头。
我添加了下面提到的两个环境variables并解决了这个问题 –
Variable Name: GRADLE_OPTS Variable Value: -Xmx512m Variable Name: JAVA_OPTS Variable Value: -Xmx512m
希望这对像我这样的人有帮助。
把下面的内容作为gradle.propertiesjoin〜/ .gradle
# Project-wide Gradle settings. # IDE (eg 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
我亲自审阅了所有的文章,但是通过这些步骤修正了它。
如果你使用的是32位的jvm,可能是这个问题安装一个64位的jvm。
- 转到控制面板(在Windows 10中searchJava)
- findJava应用程序
- 双击java,然后查看。
-
在运行时间参数中添加:
-Xmx 2048m