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

**URL:** <https://community.sonarsource.com/t/how-to-fix-java-lang-outofmemoryerror-java-heap-space-for-maven-and-bitbucket-pipelines/137989>\
**Category:** SonarQube Cloud\
**Created:** [March 29, 2025, 7:52pm UTC](https://community.sonarsource.com/t/how-to-fix-java-lang-outofmemoryerror-java-heap-space-for-maven-and-bitbucket-pipelines/137989 "2025-03-29T19:52:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nutmix](https://avatars.discourse-cdn.com/v4/letter/n/b19c9b/32.png) [@nutmix](https://community.sonarsource.com/u/nutmix)\
**Post date:** [March 29, 2025, 7:52pm UTC](https://community.sonarsource.com/t/how-to-fix-java-lang-outofmemoryerror-java-heap-space-for-maven-and-bitbucket-pipelines/137989/1 "2025-03-29T19:52:28Z")

</div>

If we run this locally:

```auto
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:

```auto
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:

```auto
[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

```

---

<div class="post-metadata">

**Author:** ![Colin](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/colin/32/55255_2.png) [@Colin](https://community.sonarsource.com/u/Colin)\
**Post date:** [March 31, 2025, 9:56am UTC](https://community.sonarsource.com/t/how-to-fix-java-lang-outofmemoryerror-java-heap-space-for-maven-and-bitbucket-pipelines/137989/2 "2025-03-31T09:56:52Z")

</div>

Hey there.

> [@nutmix](#):
>
> We tried setting:
> 
> ```auto
> SONAR_SCANNER_OPTS to -Xmx4096m
> 
> ```

You should use [SONAR\_SCANNER\_JAVA\_OPTS](https://docs.sonarsource.com/sonarqube-cloud/advanced-setup/ci-based-analysis/sonarscanner-cli/#java-heap-space-error-or-javalangoutofmemoryerror).

---

<div class="post-metadata">

**Author:** ![nutmix](https://avatars.discourse-cdn.com/v4/letter/n/b19c9b/32.png) [@nutmix](https://community.sonarsource.com/u/nutmix)\
**Post date:** [March 31, 2025, 10:35am UTC](https://community.sonarsource.com/t/how-to-fix-java-lang-outofmemoryerror-java-heap-space-for-maven-and-bitbucket-pipelines/137989/3 "2025-03-31T10:35:34Z")

</div>

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

```
Container 'Build' exceeded [memory limit]

```

These are the pipeline vars:

```auto
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?

---

<div class="post-metadata">

**Author:** ![Colin](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/colin/32/55255_2.png) [@Colin](https://community.sonarsource.com/u/Colin)\
**Post date:** [March 31, 2025, 11:34am UTC](https://community.sonarsource.com/t/how-to-fix-java-lang-outofmemoryerror-java-heap-space-for-maven-and-bitbucket-pipelines/137989/4 "2025-03-31T11:34:20Z")

</div>

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.

> [@nutmix](#):
>
> ```auto
> SONAR_SCANNER_JAVA_OPTS to -Xmx4096m
> 
> ```

> [@nutmix](#):
>
> Has anyone else had memory issues with sonar and bitbucker, and, if so, how did you allocate more?

[Take a look at the docs here](https://docs.sonarsource.com/sonarqube-cloud/advanced-setup/ci-based-analysis/bitbucket-pipelines-for-sonarcloud/#docker-memory-limit).

---

<div class="post-metadata">

**Author:** ![nutmix](https://avatars.discourse-cdn.com/v4/letter/n/b19c9b/32.png) [@nutmix](https://community.sonarsource.com/u/nutmix)\
**Post date:** [March 31, 2025, 12:01pm UTC](https://community.sonarsource.com/t/how-to-fix-java-lang-outofmemoryerror-java-heap-space-for-maven-and-bitbucket-pipelines/137989/5 "2025-03-31T12:01:04Z")

</div>

> [@nutmix](#):
>
> Has anyone else had memory issues with sonar and bitbucker, and, if so, how did you allocate more?

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.**

```

---

<div class="post-metadata">

**Author:** ![nutmix](https://avatars.discourse-cdn.com/v4/letter/n/b19c9b/32.png) [@nutmix](https://community.sonarsource.com/u/nutmix)\
**Post date:** [March 31, 2025, 12:22pm UTC](https://community.sonarsource.com/t/how-to-fix-java-lang-outofmemoryerror-java-heap-space-for-maven-and-bitbucket-pipelines/137989/6 "2025-03-31T12:22:29Z")

</div>

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.

```auto
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/**

```
