-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Degraded performance in metering when creating a lot of instruments #84713
Comments
cc @dpk83 |
@xakep139 - Thanks for issue report! As part of work for .NET 8 we were already investigating making Meter.CreateXXX() cache the instruments it creates and return those cached values if you call it with the same arguments. In the example shown above that would change it from having 100M instruments to only having one. However would the scenario in your real production code benefit from that type of caching in the same way? |
@noahfalk, thanks for the reply! Indeed, our partner service creates most of the instruments with the same name, so if it was/will be addressed in .NET 8, then it would solve the issue. Can you point me to the related issue/PR please? |
Its hidden away as a subset of the work in this issue: #77514 |
For now, can't you just cache the created instrument instead of creating it every time? |
This workaround was covered in |
We're closing this as a dupe of #77514 since the work comprised there will solve this issue. |
Description
One of our partner services discovered that there's an issue when one creates a lot of instruments (e.g.
Counter
's) and drop them on the floor. The bottleneck is here: MeterListener.cs,57.As access to
DiagLinkedList
is protected with lock, very likely there are too many nodes in the LinkedList, every time whenCreateHistogram()
orCreateCounter()
is called, a new node will be added to the LinkedList, becauseEnableMeasurementEvents()
usesobject.ReferenceEquals
comparer.So, all the references to the created instruments will be held in a
MeterListener
instance and will cause_enabledMeasurementInstruments.AddIfNotExist(instrument, object.ReferenceEquals);
to take longer and longer - while keeping the lock. That significantly increases CPU usage and causes a memory leak.Configuration
Here's a code snippet that showcases the issue:
Regression?
I tried the code above on different combinations of
net7.0
andnet6.0
vs.System.Diagnostics.DiagnosticSource
versions6.0.0
and7.0.2
.Data
Analysis
Surely this can be worked around when using a cache of created instruments.
Since the Instrument doesn't implement
IDisposable
, it's hard to figure out that it's not recommended to create a lot of instruments.The text was updated successfully, but these errors were encountered: