"Memory leak" in SqlEventListener
See original GitHub issueWe’re experiencing memory growth issues when using the SqlClientDiagnosticSubscriber. Took a dump of the running process and analyzed it in dotMemory. Turns out a lot memory is allocated by _processingSpans.
When looking at the code I see that “ended” spans are never removed from the dictionary.
Perhaps there is a good reason to leave the span in the dictionary after it has ended. I don’t know.
But if there isn’t, and the span should be removed from the dictionary, then a quick fix would be to replace
if (!_processingSpans.TryGetValue(id, out var item))
with
if (!_processingSpans.TryRemove(id, out var item))
private void ProcessEndExecute(IReadOnlyList<object> payload)
{
if (payload.Count != 3)
{
_logger?.Debug()
?.Log("EndExecute event has {PayloadCount} payload items instead of 3. Event processing is skipped.", payload.Count);
return;
}
var id = Convert.ToInt32(payload[0]);
var compositeState = Convert.ToInt32(payload[1]);
var sqlExceptionNumber = Convert.ToInt32(payload[2]);
var stop = Stopwatch.GetTimestamp();
_logger?.Trace()
?.Log("Process EndExecute event. Id: {Id}. Composite state: {CompositeState}. Sql exception number: {SqlExceptionNumber}.", id,
compositeState, sqlExceptionNumber);
if (!_processingSpans.TryGetValue(id, out var item))
{
_logger?.Warning()
?.Log("Failed capturing sql statement (failed to remove from ProcessingSpans).");
return;
}
var isSuccess = (compositeState & 1) == 1;
var isSqlException = (compositeState & 2) == 2;
// 4 - is synchronous
item.Span.Duration = ((stop - item.Start) / (double)Stopwatch.Frequency) * 1000;
if (isSqlException)
{
item.Span.CaptureError("Exception has occurred", sqlExceptionNumber != 0 ? $"SQL Exception {sqlExceptionNumber}" : null,
null);
}
item.Span.End();
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Why and How to avoid Event Handler memory leaks?
The cause is simple to explain: while an event handler is subscribed, the publisher of the event holds a reference to the subscriber...
Read more >Diagnosing Event Handler Leaks with the Memory Usage ...
Memory Usage tool in the Diagnostics Tool window In this blog post, I'll demonstrate how to use the Memory Usage tool while debugging...
Read more >Memory leak : r/csharp
I made an application that runs 24/7 on a server. It's constantly deserializing JSON files that are produce by some machines and then...
Read more >February | 2020 | Read the Tea Leaves - Nolan Lawson
Since event listeners are the most common source of memory leaks, another technique that I've used is to monkey-patch the addEventListener and ...
Read more >Fix memory problems - Chrome Developers
A page's performance gets progressively worse over time. This is possibly a symptom of a memory leak. · A page's performance is consistently...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Can confirm that 1.5.1 fixes the memory issue @gregkalapos Tested 1.5.0 and 1.5.1 with the same requests, and as you can see the spans are successfully removed from the dictionary (as expected).
With version 1.5.0
With version 1.5.1
Thank you @gregkalapos! I will test tomorrow and let you know. Really appreciate your quick response!