title: MSBuild ver.16 runs hundreds of MSBuild processes when MaxNodeCount is greater than 1
body: <p>We have an application, which builds and prepares a &quot;deploy package&quot; of our ASP.NET applications. It had references to the .NET Framework MSBuild, which was working fine. Few weeks ago we started to use VS2019 instead of VS2015. Because of some NuGet packages we had to update this application with latest MSBuild, which is now part of the VS and not in GAC anymore. I did that using <a href="https://docs.microsoft.com/en-us/visualstudio/msbuild/updating-an-existing-application?view=vs-2019" rel="nofollow noreferrer">this documentation</a>. And then the fun began... :/ I found out that the problem lies in <em>BuildParameters.MaxNodeCount</em> property:</p>
  <ul>
  <li>If I set it's value to 1, everything works fine but far slower than before the update.</li>
  <li>If value is set to something greater than 1 (I set it to <em>Environment.ProcessorCount</em>), literaly hundreds of MSBuild processes are started. Sometimes it works but most of the time it fails with <em>outofmemory</em> exception. Sometimes even OS collapses and needs to be restarted.</li>
  </ul>
  <p><a href="https://i.stack.imgur.com/ipeT2.png" rel="nofollow noreferrer">Picture of lots of MSBuild processes</a></p>
  <p><a href="https://i.stack.imgur.com/jol67.png" rel="nofollow noreferrer">Picture of MSBuild error</a></p>
  <p>Before the update <em>BuildProperties.MaxNodeCount</em> was set to <em>Environment.ProcessorCount</em> and everything worked fine.</p>
  <p>Any idea what could cause this kind of behaviour? Why property MaxNodeCount is not taken into account?</p>
  <p><strong>Edit 1:</strong></p>
  <p>Code, that invokes MSBuild API:</p>
  <pre><code>Dictionary&lt;string, string&gt; GlobalProperty = new Dictionary&lt;string, string&gt;();
  GlobalProperty.Add(&quot;Platform&quot;, &quot;Any CPU&quot;);
  GlobalProperty.Add(&quot;Configuration&quot;, &quot;Release&quot;);
  GlobalProperty.Add(&quot;TargetFrameworkVersion&quot;, &quot;v4.8&quot;);
  GlobalProperty.Add(&quot;DeployOnBuild&quot;, &quot;true&quot;);
  GlobalProperty.Add(&quot;PublishProfile&quot;, &quot;passed_as_input_parameter&quot;);
  GlobalProperty.Add(&quot;AspnetMergePath&quot;, &quot;passed_as_input_parameter&quot;);
  
  BuildResult buildResult;
  FileLogger fl = new FileLogger()
  {
      Parameters = @&quot;logfile=log_file_path;append=true&quot;,
      Verbosity = LoggerVerbosity.Normal
  };
  
  BuildParameters bp = new BuildParameters()
  {
      Loggers = new List&lt;Microsoft.Build.Framework.ILogger&gt; { fl }.AsEnumerable(),
      DetailedSummary = false,
      OnlyLogCriticalEvents = false,
      ShutdownInProcNodeOnBuildFinish = true,
      MaxNodeCount = Environment.ProcessorCount
  };           
              
  BuildRequestData br;
  br = new BuildRequestData(projectFileName, GlobalProperty, null, new string[] { &quot;Clean&quot;, &quot;Rebuild&quot; }, null);
  
  buildResult = BuildManager.DefaultBuildManager.Build(bp, br);
  </code></pre>