Java heap space in huge project

  • ALM used (Azure DevOps)
  • CI system used (Azure DevOps)
  • Scan of customized Linux distribution build inside docker container with build-wrapper
  • Languages of the repository (C/C++)
  • Error observed (Java heap space at the end of scan)

I am building customized Linux distribution inside Linux docker container and trying to scan produced files by sonar scanner through the command like this:

docker run \
      --name <name> \
      --rm \
      --volume <imagePath> \
      --env SONAR_SCANNER_OPTS="-Xmx1024m -Xms512m" \
      --env SONAR_RUNNER_OPTS="-Xmx1024m -XX:MaxPermSize=512m" \
      --env JAVA_OPTS="-Xmx1024m -Xms512m" \
      --env JAVA_EXEC="-Xmx1024m -XX:MaxPermSize=512m -XX:ReservedCodeCacheSize=128m" \
      <imageName> /bin/bash -c \
      "build-wrapper-linux-x86-64 --out-dir <containerHomeDirectory>/build-wrapper/build-wrapper-linux-x86 \
       <pathToBuildScript>
      && sonar-scanner -X"

Even if I add some environment variables to increase memory (see above) I still have got an error like: Java heap space at the end of sonar scan:

2023-09-27T12:10:59.7232822Z 12:10:59.722 INFO: Hit the cache for 0 out of 0
2023-09-27T12:10:59.7236300Z 12:10:59.723 INFO: Miss the cache for 0 out of 0
2023-09-27T12:10:59.7401480Z 12:10:59.739 DEBUG: the bridge server will shutdown
2023-09-27T12:11:04.7428624Z 12:11:04.742 DEBUG: the bridge server closed
2023-09-27T12:11:04.7428903Z 12:11:04.742 INFO: Time spent writing ucfgs 0ms
2023-09-27T12:11:05.3032575Z 12:11:05.302 INFO: ------------------------------------------------------------------------
2023-09-27T12:11:05.3032826Z 12:11:05.302 INFO: EXECUTION FAILURE
2023-09-27T12:11:05.3033214Z 12:11:05.302 INFO: ------------------------------------------------------------------------
2023-09-27T12:11:05.3033383Z 12:11:05.302 INFO: Total time: 17:48.562s
2023-09-27T12:11:05.4011755Z 12:11:05.400 ERROR: Error during SonarScanner execution
2023-09-27T12:11:05.4011979Z 12:11:05.400 INFO: Final Memory: 84M/512M
2023-09-27T12:11:05.4012568Z 12:11:05.400 INFO: ------------------------------------------------------------------------
2023-09-27T12:11:05.4012731Z java.lang.OutOfMemoryError: Java heap space
2023-09-27T12:11:05.4046575Z 	at java.base/java.lang.StringCoding.encodeASCII(Unknown Source)
2023-09-27T12:11:05.4046851Z 	at java.base/java.lang.StringCoding.encode(Unknown Source)
2023-09-27T12:11:05.4047032Z 	at java.base/java.lang.String.getBytes(Unknown Source)
2023-09-27T12:11:05.4047184Z 	at java.base/java.io.UnixFileSystem.getBooleanAttributes0(Native Method)
2023-09-27T12:11:05.4047357Z 	at java.base/java.io.UnixFileSystem.getBooleanAttributes(Unknown Source)
2023-09-27T12:11:05.4050774Z 	at java.base/java.io.File.isDirectory(Unknown Source)
2023-09-27T12:11:05.4051061Z 	at org.sonar.plugins.javascript.bridge.TsConfigProvider$LookupTsConfigProvider.tsconfigs(TsConfigProvider.java:181)
2023-09-27T12:11:05.4051330Z 	at org.sonar.plugins.javascript.bridge.TsConfigProvider.tsconfigs(TsConfigProvider.java:94)
2023-09-27T12:11:05.4051770Z 	at org.sonar.plugins.javascript.bridge.TsConfigProvider.getTsConfigs(TsConfigProvider.java:89)
2023-09-27T12:11:05.4051998Z 	at org.sonar.plugins.javascript.bridge.JsTsSensor.analyzeFiles(JsTsSensor.java:109)
2023-09-27T12:11:05.4052226Z 	at org.sonar.plugins.javascript.bridge.AbstractBridgeSensor.execute(AbstractBridgeSensor.java:73)
2023-09-27T12:11:05.4055097Z 	at org.sonar.scanner.sensor.AbstractSensorWrapper.analyse(AbstractSensorWrapper.java:62)
2023-09-27T12:11:05.4055380Z 	at org.sonar.scanner.sensor.ModuleSensorsExecutor.execute(ModuleSensorsExecutor.java:75)
2023-09-27T12:11:05.4055644Z 	at org.sonar.scanner.sensor.ModuleSensorsExecutor.lambda$execute$1(ModuleSensorsExecutor.java:48)
2023-09-27T12:11:05.4056940Z 	at org.sonar.scanner.sensor.ModuleSensorsExecutor$$Lambda$1246/0x0000000100ab8440.run(Unknown Source)
2023-09-27T12:11:05.4057300Z 	at org.sonar.scanner.sensor.ModuleSensorsExecutor.withModuleStrategy(ModuleSensorsExecutor.java:66)
2023-09-27T12:11:05.4057533Z 	at org.sonar.scanner.sensor.ModuleSensorsExecutor.execute(ModuleSensorsExecutor.java:48)
2023-09-27T12:11:05.4057841Z 	at org.sonar.scanner.scan.ModuleScanContainer.doAfterStart(ModuleScanContainer.java:64)
2023-09-27T12:11:05.4058627Z 	at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:123)
2023-09-27T12:11:05.4058921Z 	at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:109)
2023-09-27T12:11:05.4059158Z 	at org.sonar.scanner.scan.ProjectScanContainer.scan(ProjectScanContainer.java:163)
2023-09-27T12:11:05.4059371Z 	at org.sonar.scanner.scan.ProjectScanContainer.scanRecursively(ProjectScanContainer.java:159)
2023-09-27T12:11:05.4059599Z 	at org.sonar.scanner.scan.ProjectScanContainer.doAfterStart(ProjectScanContainer.java:130)
2023-09-27T12:11:05.4059818Z 	at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:123)
2023-09-27T12:11:05.4060028Z 	at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:109)
2023-09-27T12:11:05.4060341Z 	at org.sonar.scanner.bootstrap.ScannerContainer.doAfterStart(ScannerContainer.java:396)
2023-09-27T12:11:05.4060557Z 	at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:123)
2023-09-27T12:11:05.4064921Z 	at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:109)
2023-09-27T12:11:05.4065130Z 	at org.sonar.scanner.bootstrap.GlobalContainer.doAfterStart(GlobalContainer.java:127)
2023-09-27T12:11:05.4065354Z 	at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:123)
2023-09-27T12:11:05.4065565Z 	at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:109)
2023-09-27T12:11:05.4065864Z 	at org.sonar.batch.bootstrapper.Batch.doExecute(Batch.java:57)
2023-09-27T12:11:08.2308770Z ##[error]Bash exited with code '1'.
2023-09-27T12:11:08.2718355Z ##[section]Finishing: Build image

The virtual machine where this docker container is running has got up to 8GB of RAM memory so it should be enough performance here.

I’ve also configured sonar.exculusions property inside sonar-project.proporties file with the extensions which I don’t needed for analyze.

Do you have any tips or tricks how to handle this error??

Hey there.

Let’s get rid of all of these, as they aren’t actually affecting the memory provided to the scanner, which is only manipulated via SONAR_SCANNER_OPTS.

Can you try a larger value? like -Xmx2G -Xms2G?

yep, I’ve even tried with 4g and 8g (as it is the maximum performance of my docker currently) and still the same effect :confused:

Thanks.

I would suggest including the full analysis logs from a run where you’ve adjusted SONAR_SCANNER_OPTS

  • So we have more context
  • To make sure those options are really being taken into account

Looks like except setting up SONAR_SCANNER_OPTS env variable it was enough to set up properly _JAVA_OPTIONS like:

_JAVA_OPTIONS="-Xmx4G -Xms4G -XX:MaxRAMPercentage=80 -XX:MinRAMPercentage=50"

And the issue with Java heap space doesn’t exist for now.

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