473,395 Members | 1,558 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,395 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 4287
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.