Out of Memory running SonarQube Analysis on a large projects

Whilst adding SonarQube analysis to a large project I started getting memory errors during the analysis phase. The solution was to up the memory available to the SonarQube Scanner on the my build agent, not the memory on the SonarQube server as I had first thought. This is done with an environment variable as per the documentation, but how best to do this within our Azure DevOps build systems?

The easiest way to set the environment variable `SONAR_SCANNER_OPTS` on every build agent is to just set it via a Azure Pipeline variable. This works because the build agent makes all pipeline variables available as environment variables at runtime.

So as I was using YML Pipeline, I set a variable within the build job

- `job: build
timeoutInMinutes: 240
variables:

  • name: BuildConfiguration
    value: 'Release'
  • name: SONAR_SCANNER_OPTS
    value: -Xmx4096m
    steps:`

I found I had to quadruple the memory allocated to the scanner. Once this was done my analysis completed