26

I am deploying a large Java project on Sonar using "Findbugs" as profile and getting the error below:

Caused by: java.util.concurrent.ExecutionException: java.lang.OutOfMemoryError:
Java heap space

What i have tried to resolve this:

  1. Replaced %SONAR_RUNNER_OPTS% with -Xms256m -Xmx1024m to increase the heap size in sonar-runner bat file.
  2. Put "sonar.findbugs.effort" parameter as "Min" in Sonar global parameters.

But both of above methods didn't work for me.

lucrussell
  • 5,032
  • 2
  • 33
  • 39
shekhar verma
  • 489
  • 2
  • 9
  • 22

8 Answers8

15

I had the same problem and found a very different solution, perhaps because I'm having a hard time swallowing the previous answers / comments. With 10 million lines of code (that's more code than is in an F16 fighter jet), if you have a 100 characters per line (a crazy size), you could load the whole code base into 1GB of memory. I set it 8GB of memory and it still failed. Why?

Answer: Because the community Sonar C++ scanner seems to have a bug where it picks up ANY file with the letter 'c' in its extension. That includes .doc, .docx, .ipch, etc. Hence, the reason it's running out of memory is because it's trying to read some file that it thinks is 300mb of pure code but really it should be ignored.

Solution: Find the extensions used by all of the files in your project (see more here):

dir /s /b | perl -ne 'print $1 if m/\.([^^.\\\\]+)$/' | sort -u | grep c

Then add these other extensions as exclusions in your sonar.properties file:

sonar.exclusions=**/*.doc,**/*.docx,**/*.ipch

Then set your memory limits back to regular amounts.

%JAVA_EXEC% -Xmx1024m -XX:MaxPermSize=512m -XX:ReservedCodeCacheSize=128m %SONAR_RUNNER_OPTS% ...
Community
  • 1
  • 1
Ryan Shillington
  • 23,006
  • 14
  • 93
  • 108
14

this has worked for me:

SONAR_RUNNER_OPTS="-Xmx3062m -XX:MaxPermSize=512m -XX:ReservedCodeCacheSize=128m"

I set it direct in the sonar-runner(.bat) file

seoservice.ch
  • 330
  • 2
  • 9
  • for me, its giving ***Invalid maximum heap size: -Xmx512m -XX:MaxPermSize=512m -XX:ReservedCodeCacheSize=128m* Could not create the Java virtual machine.** – Denim Datta Sep 03 '13 at 14:13
  • 2
    remove "=" from the passed values – adnan kamili May 09 '16 at 08:29
  • 5
    `export SONAR_SCANNER_OPTS="-Xmx3062m -XX:MaxPermSize=512m -XX:ReservedCodeCacheSize=128m"` if you're using SonarQube Scanner 2.5+ and are on Linux – zacran Aug 10 '16 at 21:58
  • 1
    `export SONAR_SCANNER_OPTS="-Xmx3062m -XX:MaxPermSize=512m -XX:ReservedCodeCacheSize=128m"` Worked on windows too, in regards to @zacran comment. – posix99 Jun 15 '17 at 16:04
7

I had the same problem when running sonar with maven. In my case it helped to call sonar separately:

mvn clean install && mvn sonar:sonar

instead of

mvn clean install sonar:sonar

http://docs.sonarqube.org/display/SONAR/Analyzing+with+Maven

Remark: Because my solution is connected to maven, this is not the direct answer for the question. But it might help other users who stumple upon it.

schnatterer
  • 7,525
  • 7
  • 61
  • 80
Lorim
  • 547
  • 2
  • 6
  • 14
2

What you can do it to create your own quality profile with just some Findbugs rules at first, and then progressively add more and more until you reach his OutOfMemoryError. There's probably only a single rule that makes all this fail because your code violates it - and if you deactivate this rule, it will certainly work.

2

I know this thread is a bit old but this info might help someone.

For me the problem was not like suggested by the top-answer with the C++ plugin. Instead my problem was the Xml-Plugin (https://docs.sonarqube.org/display/PLUG/SonarXML) after I deactivated it the analysis worked again.

G. Ann - SonarSource Team
  • 22,346
  • 4
  • 40
  • 76
SLuck
  • 521
  • 3
  • 14
  • Option doesn't exist in current (assuming latest codebase) SonarCloud...? –  Mar 06 '21 at 21:30
1

You can solve this issue by increase the maximum memory allocated to the appropriate process by increasing the -Xmx memory setting for the corresponding Java process in your sonar.properties file

under SonarQube/conf/sonar.properties

uncomment below lines and increase the memory as you want:

For Web: Xmx5123m -Xms1536m -XX:+HeapDumpOnOutOfMemoryError
For ElasticSearch: Xms512m -Xmx1536m -XX:+HeapDumpOnOutOfMemoryError
For Compute Engine: sonar.ce.javaOpts=-Xmx1536m -Xms128m -XX:+HeapDumpOnOutOfMemoryError
Osama Jetawe
  • 2,697
  • 6
  • 24
  • 40
0

The problem is on FindBugs side. I suppose you're analyzing a large project that probably has many violations. Take a look at two threads in Sonar's mailing list having the same issue. There are some ideas you can try for yourself.

http://sonar.15.n6.nabble.com/java-lang-OutOfMemoryError-Java-heap-space-td4898141.html

http://sonar.15.n6.nabble.com/java-lang-OutOfMemoryError-Java-heap-space-td5001587.html

ppapapetrou
  • 1,653
  • 9
  • 13
0

I know this is old, but I am just posting my answer anyway. I realized I was using the 32bit JDK (version 8) and after uninstalling it and then installing 64bit JDK (version 12) the problem disappeared.

Behrooz
  • 1,895
  • 4
  • 31
  • 47