1

enter image description here

There are a lot of questions regarding how to recycle App Pool and restart App Domain. They are a little bit different from my scenario.

I have only App Domain in a App Pool. I'm trying to restart App Domain automatically using the following code, if OutOfMemoryException is thrown.

HttpRuntime.UnloadAppDomain(); 

Question:

According to my understanding of this SO anwser, I only have one App Domain in a App Pool. Therefore, clearing of App Domain should be same as clearing of App Pool because I only need to clear one memory region.

Or do I still have to clear App Pool like this?

Community
  • 1
  • 1
Win
  • 61,100
  • 13
  • 102
  • 181
  • 1
    Interesting question but I would start to worry if I had similar problem - `OutOfMemoryException` should simply not happen... If I have to write code to handle it, isn't it indication that something really wrong is happening? – Konrad Kokosa Jan 20 '14 at 22:42
  • 2
    It scares me that you're trying to cover up a bigger issue. – Simon Whitehead Jan 20 '14 at 22:45
  • `OutOfMemoryException` sometimes happens due to **legacy code**. We are moving over to new 64-bit servers soon. In the mean time, we need to do something. – Win Jan 20 '14 at 22:46
  • Obligatory Dilbert... http://dilbert.com/strips/comic/2010-03-21/ – Aaron Palmer Jan 20 '14 at 22:47
  • OOM Exceptions are a symptom of your app leaking memory. I think you should try and track that down. Moving to a 64 bit server isn't really going to solve this problem, all you're doing is delaying the problem. – Kev Jan 20 '14 at 23:02
  • What about Web Garden? You can configure to use 2-3 AppPool for the same application, and then in case of OOM, you can restart faulted AppPool without affecting others pools. But this would increase memory usage on web server – Sergey Litvinov Jan 20 '14 at 23:04
  • @SergeyLitvinov We are testing Web Garden. There is an issue with Session State missing although we store session state in SQL Server. We need to track down a little it more. Thank you for your comment! – Win Jan 20 '14 at 23:08
  • @Kev indeed 64bit is not a complete solution... but for slow leak it may buy some good years to find a solution or see app to die or see end of the world :) – Alexei Levenkov Jan 20 '14 at 23:09
  • **I agree with comments. We definitely need to fix OOM Exception. Thank you all for your concern.** – Win Jan 20 '14 at 23:12

2 Answers2

2

If you OOM caused by managed "leaks" (forgotten references to managed objects from all sorts of caches/static collections) than restating AppDomain will likely clean up the issue.

If you OOM caused by fragmentation of 32bit address space by native allocations/loaded DLLs it is unlikely to be fixed by AppDomain recycle.

Indeed in long term you need to root cause the issue, in short term IISreset may be more reliable.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
1

"Recycling" an App pool results in the same thing as an app domain reset, it's just that an App pool restart winds down one domain and starts another. So the result would be the running app domain will not accept any new requests but will finish all the requests it accepted. A new process and app domain will start up and accept any new request. The end user shouldn't notice any disruption. Compare that to an IIS Reset that effectively terminates the process and all app domains contained within, i.e. brute force.

But an OutOfMemory exception is symptomatic of a bigger issue. I assume you know that it is not a solution.

T McKeown
  • 12,971
  • 1
  • 25
  • 32
  • 2
    Hm, as I understand in terms of OS - application pool is a process (aspnet_wp, w3wp), and AppDomain is just a part of logic for that process. And App Pool restart is restart for the process, not just for AppDomains. So, if there is some unmanaged memory leaks, then AppDomain unloading won't help, and only AppPool restart should help in that case – Sergey Litvinov Jan 20 '14 at 22:59
  • I misspoke, my answer should say, when "recycling" an app pool. – T McKeown Jan 20 '14 at 23:09
  • nothing I said differs from your comment so I don't know what your point is. My answer was explaining the difference between an app pool recycle and restart, in the end the process is ended and a new starts. – T McKeown Jan 20 '14 at 23:56
  • Originally you were saying about AppPool restarting, not recycling, so i've just commented that it's not the same as AppPool restart will restart the whole process, and Recycle won't restart it. But after your edit, all is right :) – Sergey Litvinov Jan 21 '14 at 00:01