Exception in a multi threaded C# console application while trying to send telemetry to application insights

Kundan Pitroda 1 Reputation point
2022-02-15T05:42:06.013+00:00

I have a multithreaded console application. I am generating a telemetry client for each thread, however, there is just a single copy of TelemetryConfiguration used to generate each of the clients.
When the telemetry is sent to application insights, app insights got filled up with 10k such exceptions in a couple of hours.
Can anyone point out if this memory leak is due to application insights or just reporting a memory leak due to some other part of the code?

@Monalla-MSFT @Monarch @HariKrishnaRajoli-9469

Call Stack:

System.AggregateException: System.Threading.Tasks.TaskSchedulerException: at System.Threading.Tasks.Task.ScheduleAndStart (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089) at System.Threading.Tasks.Task.InternalStartNew (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089) at System.Threading.Tasks.TaskFactory.StartNew (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089) at Microsoft.ApplicationInsights.Channel.InMemoryTransmitter..ctor (Microsoft.ApplicationInsights, Version=2.20.0.103, Culture=neutral, PublicKeyToken=31bf3856ad364e35: //BASE/src/Microsoft.ApplicationInsights/Channel/InMemoryTransmitter.cs:47) at Microsoft.ApplicationInsights.Channel.InMemoryChannel..ctor (Microsoft.ApplicationInsights, Version=2.20.0.103, Culture=neutral, PublicKeyToken=31bf3856ad364e35: //BASE/src/Microsoft.ApplicationInsights/Channel/InMemoryChannel.cs:39) at Microsoft.ApplicationInsights.Extensibility.TelemetrySink..ctor (Microsoft.ApplicationInsights, Version=2.20.0.103, Culture=neutral, PublicKeyToken=31bf3856ad364e35: //BASE/src/Microsoft.ApplicationInsights/Extensibility/TelemetrySink.cs:45) at Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration..ctor (Microsoft.ApplicationInsights, Version=2.20.0.103, Culture=neutral, PublicKeyToken=31bf3856ad364e35: //BASE/src/Microsoft.ApplicationInsights/Extensibility/TelemetryConfiguration.cs:100) at Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration..ctor (Microsoft.ApplicationInsights, Version=2.20.0.103, Culture=neutral, PublicKeyToken=31bf3856ad364e35: /_/BASE/src/Microsoft.ApplicationInsights/Extensibility/TelemetryConfiguration.cs:77) at Logging.Logger+d__21.MoveNext (Logging, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: D:\Locobuzz\Services\LoggerTelemetry\Logging\Logger.cs:242) Inner exception System.OutOfMemoryException handled at System.Threading.Tasks.Task.ScheduleAndStart: at System.Threading.Thread.StartInternal (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089) at System.Threading.Thread.Start (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089) at System.Threading.Thread.Start (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089) at System.Threading.Tasks.ThreadPoolTaskScheduler.QueueTask (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089) at System.Threading.Tasks.Task.ScheduleAndStart (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)

Azure Monitor
Azure Monitor
An Azure service that is used to collect, analyze, and act on telemetry data from Azure and on-premises environments.
2,851 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. AnuragSingh-MSFT 20,431 Reputation points
    2022-02-17T09:09:10.21+00:00

    Hi @Kundan Pitroda ,

    Welcome to Microsoft Q&A! Thanks for posting the question.

    I understand that you are using ApplicationInsights to monitor your multi-threaded console application. I was not able to gather the target .NET framework from the Exception shared, but the following information is common to both .NET core and .NET framework.

    1. Regarding the usage of TelemetryClient in console application, we don't recommend creating multiple TelemetryClient instances in the application. A singleton instance is registered in DependenecyInjection container and you should get the instance of TelemetryClient by using constructor injection or by using serviceProvider.GetRequiredService<TelemetryClient>();. For more details, please refer to Application Insights for Console Application.

    2. Memory leak - It is not possible to state that you are running into memory leak scenario only by looking at one of the exceptions. You may or maynot be running into memory leak. A memory leak generally happens when your app references objects that it no longer needs to perform the desired task, which causes memory to be not claimed efficiently through garbage collection. I would suggest referring to the following links for details on how to diagnose and troubleshoot memory leak scenarios:

    >. Measure memory usage in Visual Studio
    >. Debug a memory leak in .NET Core

    Please let me know if you have any questions.

    ---
    Please 'Accept as answer' and ‘Upvote’ if it helped so that it can help others in the community looking for help on similar topics.