java.lang.OutOfMemoryError: Java heap space in jenkins sonar scan stage

Hi,
I am using jenkins plugin i.e SonarQube Scanner Version: 2.15

I have 3 projects to scan e.g xyz_server, xyz_client and xyz_plugins.

I have 3 stages in my jenkins pipeline for sonar scan of above projects.
out of 3 projects for xyz_server and xyz_plugins, every time when I run scan analysis it gets completed without any issue. but for xyz_client project I’m getting java.lang.OutOfMemoryError: Java heap space issue every time.

I’m using jenkins slave node to run pipeline.
console output shows Analysis successful for xyz_client but throws above error.
analysis gets updated on sonar server UI correctly for xyz_client but only issue is pipeline fails with java.lang.OutOfMemoryError issue.

when I compared xyz_client vs xyz_server heap memory usage while running sonar scan stage,
it is 4.5gb for xyz_client and 0.5gb for xyz_server.

below is the console output :

java.lang.OutOfMemoryError: Java heap space
	at java.base/java.lang.StringLatin1.toChars(Unknown Source)
	at java.base/java.lang.String.toCharArray(Unknown Source)
	at java.base/sun.nio.fs.UnixPath.encode(Unknown Source)
	at java.base/sun.nio.fs.UnixPath.<init>(Unknown Source)
	at java.base/sun.nio.fs.UnixFileSystem.getPath(Unknown Source)
	at java.base/java.nio.file.Path.of(Unknown Source)
	at java.base/java.nio.file.Paths.get(Unknown Source)
	at org.apache.tools.ant.DirectoryScanner.causesIllegalSymlinkLoop(DirectoryScanner.java:1806)
	at org.apache.tools.ant.DirectoryScanner.scandir(DirectoryScanner.java:1250)
	at org.apache.tools.ant.DirectoryScanner.scandir(DirectoryScanner.java:1267)
	at org.apache.tools.ant.DirectoryScanner.scandir(DirectoryScanner.java:1267)
	at org.apache.tools.ant.DirectoryScanner.scandir(DirectoryScanner.java:1267)
	at org.apache.tools.ant.DirectoryScanner.scandir(DirectoryScanner.java:1267)
	at org.apache.tools.ant.DirectoryScanner.scandir(DirectoryScanner.java:1267)
	at org.apache.tools.ant.DirectoryScanner.scandir(DirectoryScanner.java:1267)
	at org.apache.tools.ant.DirectoryScanner.scandir(DirectoryScanner.java:1267)
	at org.apache.tools.ant.DirectoryScanner.scandir(DirectoryScanner.java:1267)
	at org.apache.tools.ant.DirectoryScanner.scandir(DirectoryScanner.java:1267)
	at org.apache.tools.ant.DirectoryScanner.scandir(DirectoryScanner.java:1267)
	at org.apache.tools.ant.DirectoryScanner.scandir(DirectoryScanner.java:1267)
	at org.apache.tools.ant.DirectoryScanner.scandir(DirectoryScanner.java:1267)
	at org.apache.tools.ant.DirectoryScanner.scandir(DirectoryScanner.java:1267)
	at org.apache.tools.ant.DirectoryScanner.scandir(DirectoryScanner.java:1267)
	at org.apache.tools.ant.DirectoryScanner.scandir(DirectoryScanner.java:1267)
	at org.apache.tools.ant.DirectoryScanner.scandir(DirectoryScanner.java:1194)
	at org.apache.tools.ant.DirectoryScanner.scandir(DirectoryScanner.java:1156)
	at org.apache.tools.ant.DirectoryScanner.checkIncludePatterns(DirectoryScanner.java:954)
	at org.apache.tools.ant.DirectoryScanner.scan(DirectoryScanner.java:912)
	at org.apache.tools.ant.types.AbstractFileSet.getDirectoryScanner(AbstractFileSet.java:528)
	at hudson.FilePath.glob(FilePath.java:2167)
	at hudson.FilePath$ListGlob.invoke(FilePath.java:2143)
	at hudson.FilePath$ListGlob.invoke(FilePath.java:2128)

Also:

org.jenkinsci.plugins.workflow.actions.ErrorAction$ErrorId: 71601352-1b84-40fb-8045-12e9f5ff12ca
Caused: java.io.IOException: Remote call on JNLP4-connect connection from my_hostname.my_org.net/my_ip:port failed
	at hudson.remoting.Channel.call(Channel.java:1004)
	at hudson.FilePath.act(FilePath.java:1186)
	at hudson.FilePath.act(FilePath.java:1175)
	at hudson.FilePath.list(FilePath.java:2125)
	at hudson.FilePath.list(FilePath.java:2108)
	at hudson.FilePath.list(FilePath.java:2092)
	at hudson.plugins.sonar.utils.SonarUtils.extractReportTask(SonarUtils.java:91)
	at hudson.plugins.sonar.utils.SonarUtils.addBuildInfoTo(SonarUtils.java:131)
	at hudson.plugins.sonar.SonarBuildWrapper$AddBuildInfo.tearDown(SonarBuildWrapper.java:215)
	at org.jenkinsci.plugins.workflow.steps.CoreWrapperStep$Callback.finished(CoreWrapperStep.java:207)
	at org.jenkinsci.plugins.workflow.steps.CoreWrapperStep$Execution2$Callback2.finished(CoreWrapperStep.java:150)
	at org.jenkinsci.plugins.workflow.steps.GeneralNonBlockingStepExecution$TailCall.lambda$onSuccess$0(GeneralNonBlockingStepExecution.java:140)
	at org.jenkinsci.plugins.workflow.steps.GeneralNonBlockingStepExecution.lambda$run$0(GeneralNonBlockingStepExecution.java:77)
	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
	at java.base/java.lang.Thread.run(Thread.java:833)

following ss shows the original heap size of slave node:

following ss shows memory usage of xyz_client sonar scan process

please help me to resolve this issue

Welcome :slight_smile:

you need to configure SONAR_SCANNER_OPTS via https://yourjenkins/manage/configure
For us -Xmx4G works fine.

Gilbert

1 Like

thank you for quick suggestion,
I’ll try and update here

even after setting SONAR_SCANNER_OPTS to around 8gb it is still giving me same error

At a second glance i see you’re using Ant.
What is your ANT_OPTS setting in Jenkins ?

actually I’m not using Ant and do not have ANT_OPTS setting as well in jenkins.

Somehow the Ant api seems to be involved, maybe via Gradle (using parts of Ant under the hood) or
Groovy antbuilder !?
From the stacktrace i assume that the Ant directory scanner is responsible for the OOM.

We need some more details about your pipeline.
Your screenshot has openjdk 1.8.0_372, but the Sonar analysis needs to run with Java 11 or 17
for the latest Sonarqube versions. What’s your Sonarqube version ?
Would it be possible to post your Jenkinsfile to get more insight with sensitive parts redacted ?

could you please provide me with your email address. I’ll send you the jenkins file.

I have sent you a PM in this forum, just reply and attach the Jenkinsfile.

Jenkinsfile.txt (31.8 KB)

@anon67236913 I have attached jenkins file in text format.

I’m using SonarQube Community Edition - Version 10.0

@anon67236913 have you got chance to go through jenkins file?
have you identified the cause of the “out of memory” issue?

Hi,

still wondering about the org.apache.tools... entries in the stacktrace.
ok, you’re using sonar-scanner - npm which is not provided by Sonarsource
GitHub - bcaudan/node-sonar-scanner: Sonar scanner for node

Our npm builds all use the Sonar CLI scanner instead, the Sonar CLI scanner will respect the
SONAR_SCANNER_OPTS setting.

If using npm sonar-scanner you may try to increase the heap via
export NODE_OPTIONS="--max-old-space-size=8192" in your pipeline.

But i recommend using the Sonar CLI scanner for npm builds, means
Jenkins extension for Sonarqube 2.15

and

Sonar CLI scanner 4.8

Gilbert

1 Like

Hi @anon67236913 , I’m using sonarqube-scanner - npm (npmjs.com)

GitHub - SonarSource/sonar-scanner-npm: SonarQube Scanner for the JavaScript world

I tried NODE_OPTIONS as well but no luck. I’m getting same error.

My doubt is heap memory requirement is more than 4.5gb and if configured MaxHeapMemory = 4.2gb,
then is it possible to increase heap memory using NODE_OPTIONS or SONAR_SCANNER_OPTS?
will it limit the memory to 4.2gb because of configured MaxHeapMemory, even though we tried increasing it to 6gb or 8gb.

What’s the output of free -th ?

@Rebse this is jenkins slave node memory when no jenkins job running

What are your Jenkins Java VM settings (Xmx …) ?, see jenkins.xml

@anon67236913 we do not have jenkins.xml also there is no configuration related to jvm or java

maybe we can use Additional arguments as shown in above ss to set any additional argument.

However I tried setting SONAR_SCANNER_OPTS here as well and it did’t resolved that issue.

The jenkins.xml is located on the Jenkins controller (FKA Jenkins master), it contains the Java VM settings.
Our controller has - see https://yourjenkins/manage/configure

node_options

You may try this, but i recommend to switch to Sonar scanner CLI for npm.

I tried sonar scanner cli on my local system( windows OS) and by setting
set SONAR_SCANNER_OPTS=-Xmx4g

but it continuously scans the project and scan not completed even though waited for 4-5 hours.

I’m using docker image of sonarqube to launch sonar server

that’s why I’m not trying it on jenkins.