question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"Memory leak" in SqlEventListener

See original GitHub issue

We’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.

dotmemory

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:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
nilsgstrabocommented, May 15, 2020

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 apm_1_5_0

With version 1.5.1 apm_1_5_1

1reaction
nilsgstrabocommented, May 14, 2020

Thank you @gregkalapos! I will test tomorrow and let you know. Really appreciate your quick response!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found