473,324 Members | 2,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

OutOfMemoryException and UnhandledExceptionEventHandler

I've got a program that has no user interface (like a service but not
actually a Windows Service yet) in which I'd like to handle
OutOfMemoryExceptions. I'd at least like to log the failure before exiting,
if possible. I understand that it probably takes some memory to continue
operating, even just to write a message to an open file, but if the
allocation that triggered the OutOfMemoryException was large then there
probably really is a little memory left. I am causing/throwing the
OutOfMemoryException from a Thread other than the main one, so this is a
case where UnhandledExceptionEventHandler works for other types of
exceptions.

Unfortunately, unless I'm missing something it looks like my
UnhandledExceptionEventHandler doesn't get called for OutOfMemoryExceptions,
even if there is plenty of memory and I just threw the OutOfMemoryException
explicitly myself.

Doing a try/catch at the top of every thread (there are many) is
undesirable, plus any particular worker thread doesn't have any idea what
should be done for the whole service when this happens - so I'd have to
convert to my own ApplicationException subclass and recognize that in the
UnhandledExceptionEventHandler. I'd prefer to handle it in one place for
the entire service AppDomain.

Any ideas on good ways to handle this?

Ryan Seghers


Jul 21 '05 #1
4 4286
If your handler getting called for other exceptions? I have no idea how
stable the clr is when an OutOfMemoryException occurs - you may be very
constrained on what you can do. At the very least I would pre-allocate any
objects you need to log the exception.

I would expect that so long as your handler is subscribing to the unhandled
exception from the context of the default appdomain you should be able to
get all exceptions (except probably the ExecutionEngineException, which is
completely fatal, possibly a StackOverflowException).

"Ryan Seghers" <no*****@nowhere.com> wrote in message
news:T1*****************@news.uswest.net...
I've got a program that has no user interface (like a service but not
actually a Windows Service yet) in which I'd like to handle
OutOfMemoryExceptions. I'd at least like to log the failure before exiting, if possible. I understand that it probably takes some memory to continue
operating, even just to write a message to an open file, but if the
allocation that triggered the OutOfMemoryException was large then there
probably really is a little memory left. I am causing/throwing the
OutOfMemoryException from a Thread other than the main one, so this is a
case where UnhandledExceptionEventHandler works for other types of
exceptions.

Unfortunately, unless I'm missing something it looks like my
UnhandledExceptionEventHandler doesn't get called for OutOfMemoryExceptions, even if there is plenty of memory and I just threw the OutOfMemoryException explicitly myself.

Doing a try/catch at the top of every thread (there are many) is
undesirable, plus any particular worker thread doesn't have any idea what
should be done for the whole service when this happens - so I'd have to
convert to my own ApplicationException subclass and recognize that in the
UnhandledExceptionEventHandler. I'd prefer to handle it in one place for
the entire service AppDomain.

Any ideas on good ways to handle this?

Ryan Seghers

Jul 21 '05 #2
> If your handler getting called for other exceptions? I have no idea how
stable the clr is when an OutOfMemoryException occurs - you may be very
constrained on what you can do. At the very least I would pre-allocate any
objects you need to log the exception.
Yes, my handler gets called for other exceptions. I explicitly threw an
ApplicationException to test that.

As far as stability after an OutOfMemoryException I agree. However, I
suppose statistically speaking that on average when an allocation fails
there will still be half the amount of memory I requested left. Sure would
be nice if the CLR reserved an additional small block of memory for the
out-of-memory case. Then it could free that block and then let my exception
handler run.
I would expect that so long as your handler is subscribing to the unhandled exception from the context of the default appdomain you should be able to
get all exceptions (except probably the ExecutionEngineException, which is
completely fatal, possibly a StackOverflowException).


Apparently not. Evidently OutOfMemoryException is considered as fatal as
the other two you mentioned. When I explicitly throw an
OutOfMemoryException it doesn't get to the exception handler. When I throw
an ApplicationException in the same place and under the same circumstances
it does get to the handler.

Jul 21 '05 #3
Hello Ryan,

Thanks for your post.

Based on my experience and research, you are correct that the
OutOfMemoryException will not go to UnhandledExceptionEventHandler. I am
afraid that there is not a good method to handle this.

In addition, if the CLR throws OutOfMemoryException, all of your code will
be blown out of the water, and the CLR will terminate your process
directly. While we can catch it if the GC or your own code throws this
exception.

Also, I'd like to recommend you the following MSDN article:

Best Practices for Handling Exceptions
http://msdn.microsoft.com/library/de...us/cpguide/htm
l/cpconbestpracticesforhandlingexceptions.asp

Please feel free to let me know if you have any problems or concerns.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Jul 21 '05 #4
inline
"Ryan Seghers" <no*****@nowhere.com> wrote in message
news:cN*****************@news.uswest.net...
If your handler getting called for other exceptions? I have no idea how
stable the clr is when an OutOfMemoryException occurs - you may be very
constrained on what you can do. At the very least I would pre-allocate any objects you need to log the exception.
Yes, my handler gets called for other exceptions. I explicitly threw an
ApplicationException to test that.

As far as stability after an OutOfMemoryException I agree. However, I
suppose statistically speaking that on average when an allocation fails
there will still be half the amount of memory I requested left. Sure

would be nice if the CLR reserved an additional small block of memory for the
out-of-memory case. Then it could free that block and then let my exception handler run.
It preallocates an exception object for those types so they can ensure that
an exception of those types are always available for use. I would not
therefore assume that the runtime will call your catch handler - it may nust
use that type for logging and diagnostic purposes.
I would expect that so long as your handler is subscribing to the

unhandled
exception from the context of the default appdomain you should be able to get all exceptions (except probably the ExecutionEngineException, which is completely fatal, possibly a StackOverflowException).


Apparently not. Evidently OutOfMemoryException is considered as fatal as
the other two you mentioned. When I explicitly throw an
OutOfMemoryException it doesn't get to the exception handler. When I

throw an ApplicationException in the same place and under the same circumstances
it does get to the handler.


There must be code in the clr's exception handler looking for specific
exception types and giving them special handling (i.e. initiating an
abnormal termination) when encountered. There's supposed to be a single SEH
frame in the CLR around the entire .net application that all exceptions get
funneled through and it is likely there where the code forces the
application to terminate.

This means that there are some types that you cannot throw. At the very
least MSDN should document this because there are some exceptions defined in
modules in the CLR that are there for applications to use (e.g.
ArgumentException) and obviously there are others that are not. For these
types I am surprised that MSFT allowed user applications to even create
exceptions of this type; they could have made the constructor private so
that you could not new one of these and then provide an internal factory
method for their own use.

MSFT should document which exceptions are handled like this so that
application writers would know which exceptions they should never throw. It
would be even better if applications could not even create those exceptions.
I currently know of 3 like this; OutOfmemory, StackOverlow, ExecutionEngine.
I would like to know if there are others.

I would treat those 3 exceptions as fatal and just rethrow them if I ever
catch one (not likely), but never throw one myself.

Jul 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Per Bergland | last post by:
After many woes, I finally managed to get a stack dump of my System Service (written in C#) that insists on crashing when launched at system boot time (see below on how to get this dump - I...
0
by: Glen | last post by:
I've set up the UnhandledExceptionEventHandler delegate in a console application (Main()) to handle any exceptions that bubble up from the stack and are not handled explicitly by other try/catch...
3
by: Michael | last post by:
I have a problem with catching the OutOfMemoryException in a managed C+ application. When creating small objects on the managed heap neithe the catch handler for OutOfMemoryException nor the...
1
by: Ripul Handa | last post by:
Hi We are running IIS 5.0 cluster with cisco local director. We are running a website on 2 webservers and I have been observing that from past few days we have are getting this error message of...
1
by: SMG - Idealake | last post by:
Hi all, I am getting following error on my error, what could be the reason? Exception of type System.OutOfMemoryException was thrown. Description: An unhandled exception occurred during the...
2
by: Dave | last post by:
We just started getting this error message in our application today (stack trace below). From the OutOfMemoryException, I'm guessing it could be a memory leak. I'm making sure I'm closing all my...
4
by: Ryan Seghers | last post by:
I've got a program that has no user interface (like a service but not actually a Windows Service yet) in which I'd like to handle OutOfMemoryExceptions. I'd at least like to log the failure before...
1
by: Ashkan Daie | last post by:
Hi All, When trying to install a performance counter via InstallUtil I get the following exception: Creating performance counter category Enterprise Library Caching. An exception occurred...
13
by: Venkatachalam | last post by:
Hi, In my application I have text(flat) file as input and I have to generate an XML file. The maximum input text file size can be 900MB and gererated xml may result 2+ GB. Based on the first...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.