Do I have a memory leak in my ASP.NET Core 2.2 application? - memory-leaks

I'm not really experienced with ASP.NET Core. I was tasked to do some website that does automation but I'm having problems with the memory consumption. In time the application seems like doesn't release any memory and everytime when I refresh some page the memory usage goes up and up. And also this is my first time using dependency injection. And used transient services for all of it. Can that be relatin. Here's one of snapshot I've take.

Related

100% Memory usage on Azure App Service Plan with two Apps - working set used 10gb+

I've got an app service plan with 14gb of memory - it should be plenty for my application's needs. There are two application services running on it, each identical - the private memory consumption of these hovers around 1gb but can spike to 4gb during periods of high usage. One app has a heavier usage pattern than the other.
Lately, during periods of high usage, I've noticed that the heavily used service can become unresponsive, and memory usage stays at 100% in the App Service Plan.
The high traffic service is using 4gb of private memory and starting to massively slow down. When I head over to the /scm.../ProcessExplorer/ page, I can see that the low traffic service has 1gb private memory used and 10gb of 'Working Set'.
As I understand it, on a single machine at least, the working set should be freed up when that memory is needed on another process. Does this happen naturally when two App Services share a single Plan?
It looks to me like the working set on the low-traffic instance is not being freed up to supply the needs of the high-traffic App Service.
If this is indeed the case, the simple fix is to move them to separate App Service Plans, each with 7gb of memory. However this seems like it might potentially be just shifting the problem around - has anyone else noticed similar issues with multiple Apps on a single App Service Plan? As far as I understand it, these shouldn't interfere with one another to the extent that they all need to be separated. Or have I got the wrong diagnosis?
In some high memory-consumption scenarios, your app might truly require more computing resources. In that case, consider scaling to a higher service tier so the application gets all the resources it needs. Other times, a bug in the code might cause a memory leak. A coding practice also might increase memory consumption. Getting insight into what's triggering high memory consumption is a two-part process. First, create a process dump, and then analyze the process dump. Crash Diagnoser from the Azure Site Extension Gallery can efficiently perform both these steps. For more information.
refer Capture and analyze a dump file for intermittent high memory for Web Apps.
In the end we solved this one via mitigation, rather than getting to the root cause.
We found a mitigation strategy to our previous memory issues several months ago, which was just to restart the server each night using a powershell script. This seems to prevent the memory just building up over time, and only costs us a few seconds of downtime. Our system doesn't have much overnight traffic as our users are all based in the same geographic location.
However we recently found that the overnight restart was reporting 'success' but actually failing each night due to expired credentials. Which meant that the memory issues we were having in the question I posted were actually exacerbated by server uptimes of several weeks. Restoring the overnight restart resolved the memory issues we were seeing, and we certainly don't see our system ever using 10gb+ again.
We'll investigate the memory issues if they rear their heads again. KetanChawda-MSFT's suggestion of using memory dumps to analyse the memory usage will be employed for this investigation when it's needed.

GEN 0 Garbage climbing on Azure is this normal?

After a week of running, our asp.net webapi web app on Azure becomes quite slow. Rebooting the web app fixes the issue.
The only thing I can see that looks suspicious is the Gen 0 Garbage collection climbs constantly. Is this indicating a memory leak or is just showing the number of objects that are being deleted?
The nose dive in the image below is from when I restarted the web app.
This is not necessarily an indication of a memory leak. It's also not anything specific to Azure. Gen 0 collections will occur more often than Gen 1 and 2 - that's the way the .NET GC works to satisfy memory requirements for the process.
Now, you could still have a memory leak somewhere in your application that is causing the performance degradation. To troubleshoot this, I would recommend instrumenting your application with Application Insights or a 3rd party APM tool to understand where the degradation is occurring.
Can you check in the webjob list of your Web App if you have DaaS (diagnostic as a service).
We had this job running on our web apps, and after killing it, we no longer have GC Gen on our DotNet Core Web App...

What is normal Azure WaIISHost.exe Memory Usage?

I have recently installed NewRelic server monitoring to our Azure web role. The role is a small instance. We are on OSv4 (Win 2012 R2) using 2.2 Service Runtime.
Looking at memory usage I notice that WallSHost.exe (which I understand to be Azure related) it reported as consuming 219Mb (down from a peak of 250Mb) via NewRelic. Is that a lot of memory for it? Can I reduce it? Just seemed like a lot to be taking up.
CPU usage seems to aperiodically spike at about 4% for it. However CPU isn't really an issue as my instance rarely goes above 50%
First off, why do you care how much memory a process is taking up? All of that memory will be paged out to disk, and assuming it isn't being paged back in regularly then all it does is take up page file size which is usually irrelevant.
The WaIISHost process runs your role entry point code (OnStart, Run, StatusCheck, Changing, etc) and is typically implemented in WebRole.cs. If you want to reduce the memory size of this process then you can reduce the amount of memory being loaded by your role entry point code.
See http://blogs.msdn.com/b/kwill/archive/2011/05/05/windows-azure-role-architecture.aspx for more information about the WaIISHost.exe process and what it does.

iis Cpu is on 95% usage with very little users - on production

I have a web site and I am using iis as my web server. I noticed that on production server, the cpu reaches 95% usage pretty fast with very little users. this behaviour I don't see on my developement server. I am using visual studio to develop and iis as my local web server as well.
How much big traffic you have on production comparing to development server? How their parameters compare? Before starting a deep analysis of the application itself, I would identify all the infrastructure and environmental differences. Sometime such problems happens because of some other software, like antivirus software running in the background...
Nevertheless, because it sounds rather as a application problem, I would first check Event Viewer for errors. Then I would start from monitoring a few Performance Counters to correlate % Processor Time counter with Current Connections, Available Memory, # of Exceps Thrown / sec, % Time in GC and so on. This kind of behavior usually has a reason from the list:
excessive loops usage due to some logic error, like calling the same service again and again, trying to load or parse malfunctioned file etc. This can be analyzed with dump analysis (look below).
high CPU usage due to Garbage Collector - when memory usage is extensive (or there is a memory leak even) GC may start to consume more and more CPU fighting with the memory shortage. You will see this with memory-related performance counters.
a considerable amount of exceptions thrown (for example due to some environmental problems like network unavailability, production data difference) can also consume a lot of CPU. Event Viewer and exception-related performance counters (as they can be handled silently by your application) should be a indicator here.
To further analyze your application, I suggest to make a full memory dump during high CPU usage. You can do that with Debug Diag tool. Please refer this IIS troubleshooting guide for details.

high cpu useage on w3wp process

I am encountering an issue with my asp.net c# web application where the server is hitting very high cpu useage eg. 80%+ on w3wp process.
This has only happened recently after I made numerous changes to my application. I am fairly sure that it may be one of the changes that I have made is causing this issue with high cpu useage on the iis 7 web server.
Is it possible to analyze the process and find what exactly is causing this high useage? Or what is the mechanism for debugging such an issue.
ASP.NET Case Study: High CPU in GC - Large objects and high allocation rates:
A high CPU issue is generally one of 3 things
An infinite loop
Too much load (i.e. too many requests doing lots of small things, so
no one single thing is a culprit)
Too much churning in the Garbage Collector.
Try monitoring the % Time in GC counter and the .NET CLR Memory / # Gen 0 Collections, # Gen 1 Collections and # Gen 2 Collections.
Are you calling GC.Collect() anywhere in your code?
The problem you are describing also sounds symptomatic of High CPU in .NET app using a static Generic.Dictionary, caused by multiple threads hitting the dictionary. If that's the problem:
To resolve this timing issue you should take special care to
synchronize (lock) around access to the dictionary if there is a
possibility that you may have multiple writers working at the same
time or if there is a possibility that you write while someone else is
reading/enumerating through the same dictionary.
Related: ASP.NET Performance Monitoring, and When to Alert Administrators
The quickest way is to enable Page Tracing. This will tell you exactly how long the page took to generate, and in which methods the server spent most of it's time. This should highlight any particularly slow-running methods, allowing you to focus your troubleshooting on the problematic sections.
Simply add the following to any pages you suspect of being troublesome:
<%# Page Trace="true" %>
Now, when you navigate to that page in your browser, you will get a detailed trace at the bottom of it.

Resources