runtime: dotnet publish regularly fails with OutOfMemory after update to .NET 9

Describe the bug

I’ve updated a private project to .NET 9 and started seeing regular build failures when running dotnet publish, making it almost impossible to build the project (like 9 out of 10 runs fail). Most of the time they fail with OutOfMemory exceptions, sometimes segmentation faults. Before the build the machine is using 1.25 out of 8 GB RAM during the build it goes up to about 1.58 GB when looking at with htop, so whatever peaks OOM must be doing a huge allocation. Its a virtual ubuntu machine with no swap configured for performance. (This was preconfigured either by the VM hosting company or the ubuntu installation image and not configured by me so I assume its a common setup.)

Command line to publish is dotnet publish server -c Release -f net9.0 -r linux-x64 --self-contained false -o /game/bin

We had no issues building the same codebase in .NET 8, first few builds on .NET 9 were fine too, before it suddenly started to regularly fail with OOM. Nothing really changed on the machine side I’m aware of and I’d like to know if there’s anything I can do to diagnose this further.

To Reproduce

The git repo where this occurs is not public and I’d like to know if there’s anything I can do to diagnose this short of building a public repro.

Exceptions (if any)

core succeeded (0.5s) → build/bin/core/Release/net9.0/core.dll
gateway succeeded (5.6s) → build/bin/gateway/Release/net9.0/gateway.dll
server net9.0 failed with 1 error(s) (11.2s)
/usr/share/dotnet/sdk/9.0.100/Roslyn/Microsoft.CSharp.Core.targets(89,5): error : Out of memory.

or sometimes

core succeeded (0.6s) → build/bin/core/Release/net9.0/core.dll
gateway succeeded (0.6s) → build/bin/gateway/Release/net9.0/gateway.dll
server net9.0                                                               CoreCompile (14.9s)
../compile.sh: line 3: 170587 Segmentation fault      dotnet publish server -c Release -f net9.0 -r linux-x64 --self-contained false -o /game/bin

Further technical details

dotnet --info
.NET SDK:
 Version:           9.0.100
 Commit:            59db016f11
 Workload version:  9.0.100-manifests.3068a692
 MSBuild version:   17.12.7+5b8665660

Runtime Environment:
 OS Name:     ubuntu
 OS Version:  20.04
 OS Platform: Linux
 RID:         linux-x64
 Base Path:   /usr/share/dotnet/sdk/9.0.100/

.NET workloads installed:
There are no installed workloads to display.
Configured to use loose manifests when installing new manifests.

Host:
  Version:      9.0.0
  Architecture: x64
  Commit:       9d5a6a9aa4

.NET SDKs installed:
  7.0.410 [/usr/share/dotnet/sdk]
  9.0.100 [/usr/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 7.0.20 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 9.0.0 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 7.0.20 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 9.0.0 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

Other architectures found:
  None

Environment variables:
  Not set

global.json file:
  Not found

Learn more:
  https://aka.ms/dotnet/info

Download .NET:
  https://aka.ms/dotnet/download

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 19 (15 by maintainers)

Most upvoted comments

I’ve not been able to reproduce this regularly enough anymore to get an attempt for lldb debugging. Without m:1 this still happens, but very rarely, like one in twenty builds or something. With m:1 enabled I haven’t seen the error again.

I’ve even artificially reduced free memory from 6gb to 4gb by running some services twice on the machine and that didn’t cause the problem to become worse. So I feel like the update to 9.0.101 reduced the impact of the issue enough that I’ll not worry about it anymore. Thanks for all the suggestions and advice, I’ll keep notes in case the problem comes back, so I can do a better first analysis.

I still didn’t figure out how to do a dotnet trace collect -- dotnet build, it just hangs, and if you think thats worth looking into I can open a separate issue.

The best tools we have to trace the build process itself in a CI scenario are

  • dotnet trace for low-level perf information
  • MSBuild binlogs for investigating the MSBuild build logic used
    • these won’t really have memory usage information

For a full perf log you’d be looking at something like dotnet trace collect -- dotnet build <rest of command> to get an actual trace.

If you’re using AspNetCore, there are some performance changes in .NET 9 due to the integrated static web assets compression features, but my understanding is that those have been analyzed and addressed in more recent SDK versions (9.0.101+ I believe)

I’m not an expert on dumps, but I found a blog post that seems fairly comprehensive

Thats for Windows unfortunately, the machine it fails on is a linux machine (see original report) and also taking a dump this way would require the process crashing, which isn’t the case here since the OOM is usually caught and terminates the build “normally” without crashing. It’d require some sort of trigger to create a dump on OOM.

I know there’s dotnet trace & similar tools to record diagnostic information of applications, but no idea how to collect information about the build process itself.

Worst case, if there are no other options, I could try going for the occasional segmentation fault and try to capture the resulting core dump, but I assumed there should be builtin options to diagnose the build tools just like dotnet trace can diagnose applications.

One thing to keep in mind for dump collection is that MSBuild is multi-process, so it can be hard to figure out which node to get dumps from. If you force the build to run on a single node with the /m:1 flag, then you will know the PID to dump from.

I’m not an expert on dumps, but I found a blog post that seems fairly comprehensive: fhttps://keithbabinec.com/2018/06/12/how-to-capture-and-debug-net-application-crash-dumps-in-windows/

Perhaps try some of those suggestions and let me know if it works? I think you’re right that it could potentially be a runtime issue; they have at least some native code. That latest exception is from MSBuild, but I believe it’s just reporting that a worker node crashed hard (presumably because of the OOM as you mentioned), so it isn’t all that interesting.

From what you’ve shared, I’m pretty sure that this is either a runtime bug or a bug in a third-party package you pull in. I’m going to tentatively move this to the runtime repo to see if they can identify the problem; they certainly have people who are more knowledgeable and experienced when it comes to collecting and analyzing crash dumps than I am.