java.lang.OutOfMemoryError: Java heap space on Windows VM

Seeing Java heap space error on SonarScanner for MSBuild. Running on windows VM and “set
SONAR_SCANNER_OPTS=-Xmx8G” for 600k lines of code. Sonar server is Enterprise version.

17:08:38.528 INFO: 17:08:38.5287547 Building Type propagation graph
17:08:40.716 INFO: 17:08:40.7162716 Running Tarjan on 425321 nodes
17:08:41.356 INFO: 17:08:41.3569208 Tarjan found 424702 components
17:08:44.794 INFO: 17:08:44.7944569 Variable type analysis: done
17:08:44.810 INFO: 17:08:44.8102061 Building Type propagation graph
17:08:46.997 INFO: 17:08:46.9978542 Running Tarjan on 425948 nodes
17:08:51.278 INFO: 17:08:51.2788832 Tarjan found 424574 components
17:08:54.997 INFO: ------------------------------------------------------------------------
17:08:54.997 INFO: EXECUTION FAILURE
17:08:54.997 INFO: ------------------------------------------------------------------------
17:08:54.997 INFO: Total time: 1:05.376s
17:08:55.060 INFO: Final Memory: 17M/67M
17:08:55.060 ERROR: Error during SonarScanner execution
java.lang.OutOfMemoryError: Java heap space
        at java.base/java.util.Collections.singletonIterator(Collections.java:4701)
        at java.base/java.util.Collections$SingletonSet.iterator(Collections.java:4787)
        at com.sonar.security.analysis.A.A.B.<init>(na:145)
        at com.sonar.security.analysis.N.A(na:161)
        at com.sonar.security.I.execute(na:1096)
        at org.sonar.scanner.sensor.AbstractSensorWrapper.analyse(AbstractSensorWrapper.java:48)
        at org.sonar.scanner.sensor.ModuleSensorsExecutor.execute(ModuleSensorsExecutor.java:85)
        at org.sonar.scanner.sensor.ModuleSensorsExecutor.execute(ModuleSensorsExecutor.java:62)
        at org.sonar.scanner.scan.ModuleScanContainer.doAfterStart(ModuleScanContainer.java:82)
        at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:137)
        at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:123)
        at org.sonar.scanner.scan.ProjectScanContainer.scan(ProjectScanContainer.java:393)
        at org.sonar.scanner.scan.ProjectScanContainer.scanRecursively(ProjectScanContainer.java:389)
        at org.sonar.scanner.scan.ProjectScanContainer.doAfterStart(ProjectScanContainer.java:358)
        at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:137)
        at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:123)
17:08:55.060 INFO: ------------------------------------------------------------------------
        at org.sonar.scanner.bootstrap.GlobalContainer.doAfterStart(GlobalContainer.java:144)
        at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:137)
        at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:123)
        at org.sonar.batch.bootstrapper.Batch.doExecute(Batch.java:72)
        at org.sonar.batch.bootstrapper.Batch.execute(Batch.java:66)
        at org.sonarsource.scanner.api.internal.batch.BatchIsolatedLauncher.execute(BatchIsolatedLauncher.java:46)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at org.sonarsource.scanner.api.internal.IsolatedLauncherProxy.invoke(IsolatedLauncherProxy.java:60)
        at com.sun.proxy.$Proxy0.execute(Unknown Source)
        at org.sonarsource.scanner.api.EmbeddedScanner.doExecute(EmbeddedScanner.java:189)
        at org.sonarsource.scanner.api.EmbeddedScanner.execute(EmbeddedScanner.java:138)
        at org.sonarsource.scanner.cli.Main.execute(Main.java:112)
        at org.sonarsource.scanner.cli.Main.execute(Main.java:75)
Process returned exit code 1
The SonarQube Scanner did not complete successfully
17:08:55.122  Post-processing failed. Exit code: 1

Any help is appreciated.TIA

Hey there.

What version of SonarQube are you using?

You have to specify the heap size whenever you run your program. If you are executing on the command line, whenever you execute using "java " include a parameter: “-Xmx4g -Xmx4g” or whatever you want your heap size to be. The flag Xmx specifies the maximum memory allocation pool for a Java Virtual Machine (JVM), while Xms specifies the initial memory allocation pool. The Xms flag has no default value, and Xmx typically has a default value of 256 MB. A common use for these flags is when you encounter a java.lang.OutOfMemoryError. If you are dealing with large data sets, then its always better to initialise your Java Collections (ex. Java Arraylist) with the correct size.