OutOfMemoryError: Gitlab CI + Maven (SonarScanner) + Java multi-module Maven project

I constantly run into an OutOfMemoryError when trying to run mvn sonar:sonar in a Gitlab CI pipeline of a multi-module Maven project:

510834 [INFO] Execute PMD 6.45.0 (done) | time=58039ms
511499 [INFO] ------------------------------------------------------------------------
511499 [INFO] Reactor Summary for m***e 5.7.0-SNAPSHOT:
511500 [INFO] 
511500 [INFO] m***e ....................................... FAILURE [04:42 min] (<-- parent module!)
511500 [INFO] s***n ................................... SUCCESS [  1.879 s]
...
511510 [INFO] s***e ................................. SUCCESS [  9.232 s]
511510 [INFO] ------------------------------------------------------------------------
511510 [INFO] BUILD FAILURE
511510 [INFO] ------------------------------------------------------------------------
511510 [INFO] Total time:  08:30 min
511511 [INFO] Finished at: 2023-01-16T12:12:50Z
511511 [INFO] ------------------------------------------------------------------------
511538 [ERROR] Java heap space -> [Help 1]
511538 [ERROR] 
511538 [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
511541 [ERROR] Re-run Maven using the -X switch to enable full debug logging.
511541 [ERROR] 
511541 [ERROR] For more information about the errors and possible solutions, please read the following articles:
511542 [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/OutOfMemoryError
Cleaning up project directory and file based variables
00:00
ERROR: Job failed: exit code 1

The very first thing that I tried was different variants of what’s mentioned in OutOfMemoryError - Apache Maven - Apache Software Foundation.

In my .gitlab-ci.yml there’s the test:sonar job which is defined as follows:

...
test:sonar:
  image: registry.m***/maven-docker:latest
  stage: test
  only:
    - schedules
  variables:
    MAVEN_OPTS: "-Xmx1024m"
  script:
    - mvn clean install -DskipTests sonar:sonar

I also tried this variation:

...
test:sonar:
  image: registry.m***/maven-docker:latest
  stage: test
  only:
    - schedules
  script:
    - export MAVEN_OPTS="-Xmx1024m" && mvn clean install -DskipTests sonar:sonar

This maven-docker image is based upon the image docker:19.03-dind plus installing Java, Maven and the settings.xml pointing to our SonarQube server.

Versions used:

  • openjdk version “11.0.9” 2020-10-20
  • OpenJDK Runtime Environment (build 11.0.9+11-alpine-r1)
  • OpenJDK 64-Bit Server VM (build 11.0.9+11-alpine-r1, mixed mode)
  • Apache Maven 3.6.3
  • SonarQube: * Community Edition Version 8.9.9 (build 56886)
  • sonar-maven-plugin-3.9.1.2184

It always results in a java process running within the gitlab-ci job as follows:

/usr/bin/java -Xmx1024m -Xmx512m -classpath /usr/share/java/maven-3/boot/plexus-classworlds-2.6.0.jar ... clean install -DskipTests sonar:sonar

That second “-Xmx” parameter always overwrites my settings and resets max heap space again to 512MB.

Can anyone tell me where that -Xmx512m parameter comes from?
Or how to actually set that parameter correctly without it getting overwritten again?

In case anyone else is stumbling upon this:

The problem has been a bug in the base image we used for our docker image in the gitlab-ci.

Alpine Linux had a bug in the version that docker:19.03-dind used.
See docker - How is Maven ignoring -Xmx set through MAVEN_OPTS in openjdk:8-jdk-alpine? - Stack Overflow for reference:

This obscure problem is documented in this equally hard to find bug report. Basically, Maven on Alpine Linux is packaged with an /etc/mavenrc file containing these lines:

M2_HOME="$m2_home"
MAVEN_OPTS="\$MAVEN_OPTS -Xmx512m"

The problem lies in the second line which hard codes -Xmx to 512m. So yes, -Xmx cannot be set through MAVEN_OPTS on Alpine Linux’s vanilla Maven as of time of writing.

So that fixed our issue in that I now (after upgrading from docker:19.03-dind to docker:20.10-dind) can finally increase the heap space to > 512MB and not running into OutOfMemoryErrors anymore.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.