runtime: Incorrect value returned by `Environment.ProcessorCount` when running on GCP Cloud Run resulting in memory problems.

Description

We noticed a strange behavior of our containers deployed to Google Cloud Platform’s Cloud Run since upgrading to .NET 8. We were alerted that the containers are being terminated because they used too much memory. We usually allocate 1 CPU and 512MB/1GB Memory to all our containers and it was always enough. After ruling out memory leaks in our code we started logging GC stats and noticed that Environment.ProcessorCount returned 2 even though the container only had 1 CPU available.

That in turn seems to have caused the GC to use Server Mode, whereas according to the documentation, a single-CPU machine should always use Workstation GC.

We managed to crrect this behavior by setting the DOTNET_PROCESSOR_COUNT environment variable.

I’m not sure if anything changed to the way the processor count is being reported in .NET 8, but to my knowledg this behavior was different in prior versions.

See the related issue in the google-cloud-dotnet repo: https://github.com/googleapis/google-cloud-dotnet/issues/11404

Reproduction Steps

  1. Follow the quickstart steps described in the official Cloud Run docs: https://cloud.google.com/run/docs/quickstarts/build-and-deploy/deploy-dotnet-service
  2. Add the following line in Program.cs:
app.Logger.Log(LogLevel.Information, "Processor count: {ProcessorCount}", Environment.ProcessorCount);
  1. Deploy to Cloud Run.
  2. Observe the logs.

Expected behavior

Environment.ProcessorCount should return 1 on a single CPU container and in turn set the Garbage Collector to Workstation mode.

Actual behavior

Environment.ProcessorCount returns 2 on a single-CPU container leading to misconfigured GC.

Regression?

We started experiencing this issue upon migration from .NET 7 to .NET 8.

Known Workarounds

Set DOTNET_PROCESSOR_COUNT to 1 or force Workstation GC explicitly in .csproj/.fsproj.

Configuration

Using .NET8 running in the official Asp NET Core 8.0 docker image. Architecture is x64. Image is run by Google Cloud Platform Cloud Run.

Other information

No response

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 4
  • Comments: 19 (12 by maintainers)

Most upvoted comments

I confirm 👍

Yes, the workaround is easy to put in place

However, finding that the workaround is needed is very hard (i.e you have OOM errors, so finding that you need to change the CPU count is not the most easy to find and to understand…)

we would need a good case to back port. Assume the workaround is easy enough?

It will be in .NET 9. I have opened a PR to propose backporting the fix to .NET 8: #99563

Thanks @AustinWise for your fix, it is now merged.

I filed a bug in the internal Google issue tracker for this issue and it is in the process of being triaged.

That said, I also think .NET could be improved to detect cgroup CPU limits in more cases. I have open #99508 to fix the problem. I tested this patch with .NET 8 branch and confirmed that it fixed the CPU count when run in a Google Cloud Run generation 1 environment.

Is this normal output in any circumstance? Thread(s) per core: 0

Might this be hyperthreading? You are allocated one physical core, which maps to 2 logical cores.

in case of HP, lscpu usually shows Thread(s) per core: 2