Increasing your heap size with Gradle to run memory hungry tests

Originally posted 2020-11-25

This week I had to generate a 1.7 GB JSON file. To make the code easy to run and iterate on in my IDE I set it up as a JUnit test and fired it off.

Then I ran out of memory and the test exited. Then my IDE started getting flaky.

My IDE, IntelliJ, prompted me to increase its heap size. I did that twice and my IDE was fine but the test kept running out of RAM.

Then I updated the JVM settings in my run configuration and thought I set up the test I was running with -Xmx16384m. It still failed at the same point with the same out of memory error. I could even see that the process was using the flags. Agony.

But then I noticed a second process running called the Gradle Test Executor 1. This process only had a puny 512 MB of RAM allocated to it. Finally I realized the following:

  • My IDE needed more RAM to inspect the partial large output files my code generated. That's why changing its heap size prevented it from getting flaky.
  • If I was running my code as a process I'd need to increase the max heap size there too. And that's what I did with the run configuration.
  • If I'm running my code as a test then Gradle will start the test process with some default settings. It does not inherit the parent JVM's flags.

In the end, all I had to do was add this to my build.gradle and I was all set:

test {
  maxHeapSize = "16384m"
Groovy version