Introduction

In one of my clients projects we were working with Gradle and building the application on the console with gradlew clean build.

On each build Gradle runs all tests and while the application was growing, the amount of tests also increased accordingly. The application has a few thousand tests and at some point we were receiving OutOfMemoryExceptions with increasing frequency.

The solutions

After some analyzing we pined down the cause to the - you guessed it - execution of the tests and their low memory.

There are two solutions, which you can use to solve this problem. Booth of them have to be inserted into the test{}-block.

Setting the jvmArgs

test {
  jvmArgs = ['-Xmx2048m']  
}

Setting the heapSize directly

Gradle offers you the possibility to set the heapSize directly by the use of the keyword maxHeapSize.

test {
  maxHeapSize = "2048m"
}