Skip to content

GCAllocationTick_V2 ETW events are reporting wrong allocation sizes in multithreaded application #45375

@Temp1ar

Description

@Temp1ar

Assume we have a program which is allocating fixed amount of memory(~200MB) in a tight loop with portions of 200KB in multiple threads:

using System.Linq;
using System.Threading;
class Program
{
  public static void Main(string[] args)
  {
    var threadsCount = args.Any() ? int.Parse(args[0]) : 4;
    var waitHandles = new WaitHandle[threadsCount];
    int counter = 1024;
    for (int i = 0; i != threadsCount; ++i)
    {
      var waitHandle = new AutoResetEvent(false);
      var thread = new Thread(() =>
      {
        while (Interlocked.Decrement(ref counter) > 0)
        {
          var _ = new byte[200 * 1024];
        }
        waitHandle.Set();
      });
      thread.Start();
      waitHandles[i] = waitHandle;
    }
    WaitHandle.WaitAll(waitHandles);
  }
}

If I run this program on .NET Core 5 (Windows 10) with one thread I'll get 1023 events with 204 848 bytes allocated each which is expected. Sum of allocation sizes column: ~210MB (see PerfView status bar in the bottom). If I ran multiple times - I get same numbers:

image

However If I run this program with 4 or 8 threads, allocation sizes will start to differ and total sum will not be equal to 200MB. It fluctuates between 70(Release configuration) to 360MB. Which can't be right:

image

Should there be thread synchronization on this allocation size counter or synchronization is not viable option because of low overhead requirement?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions