Gradle Idea Task Optimization

Gradle Idea Task: Tips and Trick

Do you think that running gradle task for generating full idea project and waiting so long that it takes forever to complete? Now there's a solution for that! Running gradle idea task will finish faster than waiting your pg service build finished!
Disclaimer: This solution result will vary depending on your current system configuration, hardware specification, current memory usage, whether Meltdown and Spectre mitigation patch enabled, and radioactive/quantum event on your machine luck.

What causing it so slow?

Gradle idea task process is scale linearly with total submodule within your project. With our current repo that ever-increasing, gradle idea task process will be slowed down as well. Memory consumption is also bigger (compared to codebase one year earlier). If your JVM setting is too low (or too big), this will make your gradle task slow (because of JVM GC being fired too often and eventually crash because OOM event) or your system freeze (because of high memory pressure and aggresive swap activity).

So, what to do?

Using correct JVM parameter and freeing your memory is your only viable choice for now. Always close your background task prior to executing gradle idea task, and watch memory usage. All steps will explained in the section below.

Detailed steps

org.gradle.jvmargs=-Xms1440m -Xmx14400m -XX:ReservedCodeCacheSize=256m -XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=384m -XX:+UseConcMarkSweepGC -XX:ConcGCThreads=8 -XX:ParallelGCThreads=8 -XX:+CMSParallelRemarkEnabled -XX:+UseCMSInitiatingOccupancyOnly -XX:+ScavengeBeforeFullGC -XX:+CMSScavengeBeforeRemark

Next steps

After your gradle idea task done, your IntelliJ IDEA will freezing on the first load. You can kill it and try to load it again, or wait until all indexed cache cleared and refreshed by IDEA. If your IDEA is still freezes after this steps, I recommend to update your IntelliJ IDEA to current latest version, increase IDEA JVM heap memory to 8GiB, and using Concurrent Mark Sweep GC.

References

http://jvmmemory.com
https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/cms.html
https://www.oracle.com/technetwork/articles/java/vmoptions-jsp-140102.html
Personal experiences