Where does the maximum heap size get set when running a unit test with Gradle?

Tags: , ,



I had a failing OOM unit test and wondered why I needed to manually allow for bigger maxHeapSize like so:

test {
  maxHeapSize = '4G'
}

I assumed the max heap is not capped and couldn’t find anything in gradle api sources to prove me wrong. However, running gradle with --info clearly reveals otherwise:

Starting process 'Gradle Test Executor 427'. Working directory: [project-dir] Command: /Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/bin/java -Djava.security.manager=worker.org.gradle.process.internal.worker.child.BootstrapSecurityManager -Dorg.gradle.native=false -javaagent:build/tmp/expandedArchives/org.jacoco.agent-0.8.6.jar_a26f6511a7813217be4cd6439d66563b/jacocoagent.jar=destfile=build/jacoco/test.exec,append=true,inclnolocationclasses=false,dumponexit=true,output=file,jmx=false -Xmx512m -Dfile.encoding=UTF-8 -Duser.country=US -Duser.language=en -Duser.variant -ea -cp /Users/[user]/.gradle/caches/6.4/workerMain/gradle-worker.jar worker.org.gradle.process.internal.worker.GradleWorkerMain 'Gradle Test Executor 427'

My question is where does the -Xmx512m above come from?

Next to that, is there a way to allow for unlimited max heap and would that be a totally unreasonable thing to do?

possible duplicate of Default maximum heap size for JavaExec Gradle task

Answer

The 512m default is set in DefaultWorkerProcessBuilder. 512m is a tradeoff, working for small to medium sized projects and given that multiple workers can be running simultaneously. According to the commit message:

Limit memory for worker processes by default

Our workers for compilation, testing etc. had no default memory settings until now, which mean that we would use the default given by the JVM, which is 1/4th of system memory in most cases. This is way too much when running with a high number of workers. Limiting this to 256m should be enough for small to medium sized projects. Larger projects will have to tweak this value.


Next to that, is there a way to allow for unlimited max heap and would that be a totally unreasonable thing to do?

At the time of writing, I am not aware of any JVM implementation allowing for unlimited memory and I honestly don’t think it is advisable since it can slow down the system considerably.



Source: stackoverflow