473,604 Members | 2,487 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

OutOfMemoryExce ption and UnhandledExcept ionEventHandler

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
OutOfMemoryExce ptions. 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 OutOfMemoryExce ption was large then there
probably really is a little memory left. I am causing/throwing the
OutOfMemoryExce ption from a Thread other than the main one, so this is a
case where UnhandledExcept ionEventHandler works for other types of
exceptions.

Unfortunately, unless I'm missing something it looks like my
UnhandledExcept ionEventHandler doesn't get called for OutOfMemoryExce ptions,
even if there is plenty of memory and I just threw the OutOfMemoryExce ption
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 ApplicationExce ption subclass and recognize that in the
UnhandledExcept ionEventHandler . 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 4320
If your handler getting called for other exceptions? I have no idea how
stable the clr is when an OutOfMemoryExce ption 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 ExecutionEngine Exception, which is
completely fatal, possibly a StackOverflowEx ception).

"Ryan Seghers" <no*****@nowher e.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
OutOfMemoryExce ptions. 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 OutOfMemoryExce ption was large then there
probably really is a little memory left. I am causing/throwing the
OutOfMemoryExce ption from a Thread other than the main one, so this is a
case where UnhandledExcept ionEventHandler works for other types of
exceptions.

Unfortunately, unless I'm missing something it looks like my
UnhandledExcept ionEventHandler doesn't get called for OutOfMemoryExce ptions, even if there is plenty of memory and I just threw the OutOfMemoryExce ption 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 ApplicationExce ption subclass and recognize that in the
UnhandledExcept ionEventHandler . 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 OutOfMemoryExce ption 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
ApplicationExce ption to test that.

As far as stability after an OutOfMemoryExce ption 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 ExecutionEngine Exception, which is
completely fatal, possibly a StackOverflowEx ception).


Apparently not. Evidently OutOfMemoryExce ption is considered as fatal as
the other two you mentioned. When I explicitly throw an
OutOfMemoryExce ption it doesn't get to the exception handler. When I throw
an ApplicationExce ption 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
OutOfMemoryExce ption will not go to UnhandledExcept ionEventHandler . I am
afraid that there is not a good method to handle this.

In addition, if the CLR throws OutOfMemoryExce ption, 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/cpconbestpracti cesforhandlinge xceptions.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*****@nowher e.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 OutOfMemoryExce ption 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
ApplicationExce ption to test that.

As far as stability after an OutOfMemoryExce ption 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 ExecutionEngine Exception, which is completely fatal, possibly a StackOverflowEx ception).


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

throw an ApplicationExce ption 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.
ArgumentExcepti on) 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
2759
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 couldn't find any info on how to do this). Here's the stack trace from cordbg: Unhandled exception generated: (0x04719c94) <System.Runtime.Remoting.RemotingException> _className=<null> _exceptionMethod=<null>
0
1907
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 blocks. The delegate method logs the exceptions and treats them as unrecoverable, since they are not known at design time, and exits the application. The problem I have is that even in the release version of the application, the JIT debugger...
3
5980
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 UnhandledExceptio handler is called in an out of memory situation. Instead the applicatio writes 'Fatal out of memory' to the console and exits Is this a bug in the runtime or am I missing something Here is the example code #include "stdafx.h...
1
3437
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 and on Error Messag Remote IP:66.122.242.6 Host:216.211.212.2
1
4971
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 execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.OutOfMemoryException: Exception of type System.OutOfMemoryException was thrown.
2
3930
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 connections in the finally block but I'm not sure what I should be doing. As far as the "Unable to serialize the session state" error, this app has been running for days and this is the first I've seen this one as well. Any thoughts would be...
4
322
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 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...
1
3879
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 during the Install phase. System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was
13
12931
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 column value from the text file, the row will be moved to any of those 23 DataTable which are created onfly. For eg. 01;- data for Table 1-
0
7997
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7929
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8409
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8065
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8280
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6739
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5882
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5441
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3955
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.