The latest comment was helpful.
On my side, we were having this issue on our Github CI. We increased our Java memory options like so:
- name: Set Java memory options run: echo "GRADLE_OPTS=-Xms1g -Xmx25g -XX:MaxMetaspaceSize=512M" >> $GITHUB_ENV