MVC 4 website memory leak detecting - asp.net

i have a MVC4 website. When i running it on the Hosting i got very high memory
Process Info {2 processes)
PID/Name: 96148 - w3wp
Start/Uptime: 12/15/2014 8:41:53 AM - 00:01:26
CPU Time: 00:02:18
Working set (memory): 1.41 GB
How can i figure out the problem? I do not have experience with memory leak. My website is working well on Local. I think because online site have many users access (i have around 1.5m page views per month).

There are too many possibilities. Normally, if your application cache a lot in SessionState or Runtime.Caching, it'll eventually run out of memory.
Without seeing your application, we cannot tell what causes the issue. The first thing I'll do is to profile with Memory Profiler.
For example -
Red-Gate - ANTS Memory Profiler
JetBrain - dotTrace
Telerik - JustTrace

Related

High App Pool Memory Usage Causes ASP.net Site To Have Slow Response Time

We have an ASP.net MVC web application that uses a lot of memory (4GB) after it has run a few days. The server we are running it on has plenty of power to spare (CPU running about 10% and 7% memory usage), but for some reason, as the memory in the App Pool increases, the response time of the site lowers.
Even pages that are cached are taking a very long time to load. These cached pages should be served up from memory, which should be instant. We cache a lot of pages and a lot of database calls, so it doesn't surprise me that the App Pool is that big, but it doesn't make any sense to me that the more we cache, the longer it takes for the site to respond.
If I recycle the App Pool, the site is super fast again. We are using Windows 2012, IIS 8, and SQL 2012.
Does anyone have any ideas why this might be happening?
Thanks so much!
This is a pretty broad question but I would suggest installing something like New Relic on the server to try and find bottlenecks or code that is causing memory leakage.
It will give you a report of intensive functions.
You can get a pretty decent understanding of what is going wrong during the free premium trial (no card or anything needed).

High memory usage with w3wp application pool IIS 7

I have a website application running in it's own application pool on IIS 7.0. The application is an ASP.NET MVC 3 website.
I have noticed the memory usage for this applications corresponding w3wp IIS worker service is quite high ( 800 MB, with some fluctuation ).
I am trying to diagnose the problem and have tried the following:
I have disabled output page caching for the website at IIS level and then recycled the application pool. This causes the w3wp process to restart. The memory usage for this process then slowly creeps up to around 800 MB, it takes around 30 seconds to do so. There are no page requests being handled at this time. When I restart the website from IIS the memory size of the process does not alter.
I have tried running a debug copy of the application from VS 2010, there are no problems with memory usage.
Some ideas I have/questions are:
Is this problem related to the websites code? - Given that the memory rockets before any page requests have been sent/handled, I would assume this is NOT a code problem?
The application built in MVC has no handling of caching written into it.
The website uses real-time displaying of data, it uses ajax requests periodically, and is generally left 'open' for long periods of time.
Why does the memory usage rocket up after the application is recycled and no user requests are being sent? Is this because it is loading old cache information into it's memory from disk?
The application does NOT crash, I'm just concerned about the memory usage, it is not that big of a website...
Any ideas/help with getting to the bottom of this problem would be appreciated.
The best thing to do if you can afford to use a debugger is install the Windows Debugging Tools and use something like WinDbg and SOS.dll to figure out exactly what is it in memory.
once you've installed the tools then you can:
Launch Windbg.exe running elevated (as Administrator)
Use "File->Attach To Process" and choose w3wp.exe for the app you are trying to figure out. If you have many you can use Task Manager and add the command-line column to see the PID or use IIS Manager->Worker Processes to figure it out, and then choose that process in WinDBG.
run:
.loadby sos clr
!dumpheap -stat
At that point you should be able to see all types sorted by the most memory consumption so you can start with the ones at the bottom. (I would recommend exclude Strings, and Object since those are usually a side-effect and not the cause).
Use "!dumpheap -type type-here" to find the instances and use !gcroot for those to figure out why they are in memory, maybe due to a static field, or an event handler leaked, WCF channels not disposed, or things like that are common sources.
I just looked my server and my pools use 900-1000 MB Virtual size Memory, and 380 MB Working set. My sites run smooth with out problem for some years now, and I have checked the sites from all sides. My pool never recycles and the server runs until the next update continuously with 40% stable free physical memory.
If your memory is not continuously growing, then this memory is the code plus the data that you set as static, const, the string, and the possible cache, inside your application.
You can use process explorer to see the working and the virtual size memory.
You can also think to run a profile against your code to see if you have any "memory leak" or other issue. Find one from google: https://www.google.com/search?hl=en&q=asp.net+memory+profiler.
It probably doesn't apply here but thought I would throw it in for good measure. Recently I had a problem where my memory would go right up and max out when it really could of cleaned up 80% of it. Problem: It thought it about 2 more gig than it actually did so the GC was quite lazy. (It was due to a VM ware bug -windows was reporting 8 Gig but physically there was only 6.4). See blog.http://www.worthalook.net/2014/01/give-back-memory/
Something that might help: if you "rewrite" (open/save) the web.config , then your application will reset, you should monitor the memory usage from that point. If it keeps growing during usage, this could mean memory leak or insane caching. You might be able to identify which actions on your site lead to memory increase. During a long time the memory usage of an application should be stable.

Asp.net Application throwing System.OutOfMemoryException in Windows server 2003

I am using Neo load to test the performance of my ASP.NET 2.0 application. The application works fine until the memory of the w3wp.exe process reaches about 800000K and then it starts throwing System.OutOfMemoryException. The ASP.NET application is hosted in Windows server 2003 SP2 machine and the machine has 4GB of RAM. How can i get to know the reason for this error.
The 800000K threshold is also mentioned (as 800MB) in the following article:
Troubleshooting System.OutOfMemoryExceptions in ASP.NET
When your worker process approaches 800MB in private bytes, your
chances of seeing an OOM condition begin to increase simply because
the chances of finding a large, contiguous piece of memory within the
2GB address space begin to decrease significantly.
The article then goes on to cover common scenarios that lead to this situation, such as the usage of large data sets, runaway session or application data storage, usage of debuggable assemblies, lots of exceptions thrown and even certain uses of regular expressions.
It ends with some pointers to resources to help troubleshooting Out of Memory exceptions, like the Debug Diagnostic Tool and information about garbage collection in .NET. But the most valuable resource may be this list of 21 blog posts from an ' escalation engineer' at Microsoft's ASP.NET developer support team:
MSDN Blogs > If broken it is, fix it you should > The 21 most popular blog posts

ASP.NET CPU Spike related to high "% Time in GC" - what is a good baseline for ASP.NET?

I have a situation where an ASP.NET application causes w3wp service cpu usage to spike to 100% while at the same time the "%time in GC" performance counters shows very high numbers 20-70%. This happens from time to time.
This all started happening after I upgraded to .NET 4.0 from 2.0, BTW.
I am looked at the performance counters, trying to get more clues, but I lack a good base line... what is "normal"?
#Gen 0 Collections 10,000
#Gen 1 Collections 4,700 (seems high)
#Gen 2 collections 1,200 (again seems high)
Finalization Survivors 5000 (is this high??)
To give you an idea of how busy the app is :
ASP.NET Requests / Sec: 170
ASP.NET Sessions Active: 900
On each page request, in my dev environment, Finalization Survivors is about 50. Seems high, but all the profiling tools I looked at, dont show anything out of ordinary...
I am looking for some baseline on what are the typical values for these params for a basic asp.net app. For example, how many "Finalization Survivors" are simply due to ASP.NET?
Many thanks!
UPDATE - rolled back to .net 2.0 "fixed" the problem
I have been struggling with this issue since morning, ever since I upgraded to .NET 4.0
I rolled back my chnages - set the website to use asp.net 2.0, rolled back some binaries that were compiled with .net 4.0 and all is beautiful now - no spikes, no long GC time, but still a heck of a lot of finalization survivors..
**UPDATE 2 - just switching to .NET 4.0 in IIS causes this slow down
I am updating this in case this is helpful to someone else one day who has the same problem.
I found that the problems start just after switching IIS to use ASP.NET 4.0 on the website, without changing any of the .net 2.0 website code. I assumed, perhaps incorrectly, that it is backward compatible therefore a .net 2.0 website and resources it references should work fine under asp.net 4.0 but there appears to be some issue. If I get time to investigate further, I will update this with my findings
I would suggest taking a look at ANTS Performance Profiler and ANTS Memory Profiler to see what might be causing the spike.
I have seen that when reaching server capacity or a mass app pool recycle is happening. Here is great article you can follow to identify bottlenecks:
Chapter 17 — Tuning .NET Application Performance
Check your disk space and how you Virtual memory and pagefile.sys is configured to expand. % time in GC may spike if you have very low disk space on the disk where you have placed the pagefile.sys. This is possibly because Least recently used Gen2 and LOH cant be paged as there is no space to expand and they occupy Ram leaving Gen1 and 2 to cram for space. This kicks in GC frequently and hence CPU spikes.

How to measure memory usage for a Live ASP.NET MVC web application?

So right off the bat, not sure if this question is better suited for another StackExchange site.
I've got an ASP.NET MVC 3 web application running on Windows Server 2008 and IIS 7.5
Site runs fine initially, but i can see the memory usage gradually growing. After about 12 hours it's nearly out of memory and the site chokes.
I'm using a lot of caching, so i'm thinking this combined with some possibly memory leaks is the cause of the issue.
So my question - what's the best way (tools, for example) to monitor memory usage on a web server running ASP.NET MVC?
In the past i've used good old' perfmon and put the IIS counters on to measure these things.
It this still the best way, and if so, can someone recommend a good perfmon counter template for my scenario?
Perfmon's counters are still a good technique (and free!).
PAL (Performance Analysis of Logs), a free tool, has an ASP.NET perfmon counter template for general health (in addition to generating reports of counter log files based on thresholds).
Check out:
.NET Debugging Demos Lab 7: Memory Leak
.NET Memory Leak Case Study: The Event Handlers That Made The Memory Baloon
Tracking down managed memory leaks (how to find a GC leak)
Determine if your .NET Application has a Memory Leak
Commercial tools like MemProfiler, RedGate's memory profiling tool and JetBrains Profiler are all very good (and all have free trials).

Resources