Skip to content

Memory leak with System.Diagnostics.Metrics.Meter when not disposed #114601

@hendriklhf

Description

@hendriklhf

Similar to #114599. When creating a Meter, it's added to a static List<Meter> and disposing it will remove it from the list, but when Dispose is never called, the Meter will stay in the list forever and never be collected.

lock (Instrument.SyncObject)
{
s_allMeters.Add(this);
}

lock (Instrument.SyncObject)
{
if (Disposed)
{
return;
}
Disposed = true;
s_allMeters.Remove(this);
instruments = _instruments;
_instruments = new List<Instrument>();
}

Reproduction:

using System.Diagnostics.Metrics;

for (int i = 0;; i++)
{
    Meter m = new("");
    if (i % 1_000_000 == 0)
    {
        Console.WriteLine(Environment.WorkingSet.ToString("N0"));
    }
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions