Debug / understand java-heap issues with gradle builds

We have a new version of an annotation processor we seem to run into regular out of java heap issues and a assume somehow a memory leak in the annotation processor.

Is there any way to create a thread dump / likes when this happens with a gradle build, so I can look at it?