How to fix "java.lang.OutOfMemoryError: Java heap space" for maven and bitbucket pipelines

If we run this locally:

export SONAR_TOKEN=xxx
 mvn verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=ourproject

It works fine.

however, using bitbucket pipelines (from the wizard) and maven, after it has compiled the code and run the tests correctly, it then fails with:

“Exception in thread “main” java.lang.OutOfMemoryError: Java heap space”

We tried setting:

SONAR_SCANNER_OPTS to -Xmx4096m

and

MAVEN_OPTS to -Xmx4096m

In repository variables, but this didn’t make a difference.

We tried from -Xmx1024m right up to -xMx8192m which made no difference. It runs fine locally without setting any varibles.

The pipeline is:

image: maven:3-jdk-11

clone:
  depth: full              # SonarQube Cloud scanner needs the full history to assign issues properly

definitions:
  caches:
    sonar: ~/.sonar/cache  # Caching SonarQube Cloud artifacts will speed up your build
  steps:
    - step: &build-test-sonarcloud
        name: Build, test and analyze on SonarQube Cloud
        caches:
          - maven
          - sonar
        script:
          - mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar
        artifacts:
          - target/**

pipelines:                 # More info here: https://confluence.atlassian.com/bitbucket/configure-bitbucket-pipelines-yml-792298910.html
  branches:
    master:
      - step: *build-test-sonarcloud
  pull-requests:
    '**':
      - step: *build-test-sonarcloud

Does anyone have any suggestions?

This is the error:

[INFO] Sensor JavaSensor [java]
[INFO] Configured Java source version (sonar.java.source): 8, preview features enabled (sonar.java.enablePreview): false
[INFO] The Java analyzer is running in a context where unchanged files can be skipped. Full analysis is performed for changed files, optimized analysis for unchanged files.
[INFO] Server-side caching is enabled. The Java analyzer was able to leverage cached data from previous analyses for 7 out of 198 files. These files will not be parsed.
[INFO] Using ECJ batch to parse 132 Main java source files with batch size 53 KB.
[INFO] Starting batch processing.
[INFO] 7% analyzed
[INFO] 7% analyzed
[INFO] 7% analyzed
[INFO] 7% analyzed
[INFO] 7% analyzed
[INFO] 7% analyzed
[INFO] 7% analyzed
[INFO] 7% analyzed
[INFO] 7% analyzed
[INFO] 7% analyzed
[INFO] Optimized analysis for 1 of 11 files.
[INFO] 7% analyzed
[ERROR] [stderr] Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

Hey there.

You should use SONAR_SCANNER_JAVA_OPTS.

Thanks for the reply. I tried with all 3, and now get this:

Container 'Build' exceeded [memory limit]

These are the pipeline vars:

SONAR_SCANNER_OPTS to -Xmx4096m
MAVEN_OPTS to -Xmx4096m
SONAR_SCANNER_JAVA_OPTS to -Xmx4096m

Its not a particularly big java project.

I have not been able to find how much memory the pipeline provides by default. Some posts suggests its 1024, some suggest its 4096. If its the latter maybe I can try setting SONAR_SCANNER_JAVA_OPTS to -Xmx3000m or similar.

Has anyone else had memory issues with sonar and bitbucker, and, if so, how did you allocate more?

Well, I suggest you just stick with this single one. Not sure what will happen if all the processes think they can eat up to 4Gb of RAM.

Take a look at the docs here.

Thanks. I saw those docs, and many others which show how to increase memory used by docker service, but not how to increase containers memory. We are not using docker.

There are plenty of posts if the error had been:

 **Container 'Docker' exceeded memory limit.**

But virtually no posts for:

 **Container 'Build' exceeded memory limit.**

I seem to have found a solution with “size:2x”

I removed all the OPTS settings to let java chose the heap size based on available mem.

The downside of this is that it costs 2x the build minutes, so 20 mins instead of 10.

definitions:
  caches:
    sonar: ~/.sonar/cache  # Caching SonarQube Cloud artifacts will speed up your build
  steps:
    - step: &build-test-sonarcloud
        size: 2x
        name: Build, test and analyze on SonarQube Cloud
        caches:
          - maven
          - sonar
        script:
          - mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar
        artifacts:
          - target/**