开发者

Out Of Memory Exception in WinForms

开发者 https://www.devze.com 2023-04-01 01:42 出处:网络
I created a TaskBar application in Visual Studio which minimizes i开发者_JAVA百科tself inside the system tray and show notifications to the user based on the database change. Whenever a new task assig

I created a TaskBar application in Visual Studio which minimizes i开发者_JAVA百科tself inside the system tray and show notifications to the user based on the database change. Whenever a new task assigned to the user he will notified at the system tray like a balloon popup. But whenever the applications runs for an average 15 hours time suddenly my applications crashes and shows out of memory exception. So i have some questions please answer them.

  1. How can i debug this issue ? I can't wait for 15 hours and check for this issue ?
  2. Is there any tool available to check the memory leakage of my application which can directly point at my issue ?
  3. How can i generate out of memory exception to my program so it will be debugged easily and fast ?
  4. What should i do to avoid memory leakage in my code ?

Image of my application :

Out Of Memory Exception in WinForms


In response to your 4 questions:

1: You can intercept the My.Application.Startup event to add some exception-handling code. Note that the code for the Startup event handler is stored in the ApplicationEvents.vb file, which is hidden by default. Here is some C# code (almost identical in VB) that will setup your exception handlers:

// Event handler for handling UI thread exceptions.
Application.ThreadException += 
    new ThreadExceptionEventHandler(App_UiThreadException);

// Force all Windows Forms errors to go through our handler.
// NB In .NET 4, this doesn't apply when the process state is corrupted.
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

// Event handler for handling non-UI thread exceptions. 
AppDomain.CurrentDomain.UnhandledException += new 
    UnhandledExceptionEventHandler(App_NonUiThreadException);

// Run the application.

2: You can use the free CLR Profiler to check memory usage.

3: You can throw the OutOfMemory exception yourself if you want to see how your program will handle it.

4: One useful resource is Debug Leaky Apps. Another one is Identify CLR Memory Leaks.


You can examine the stack trace as follows (posted as an answer so it would format correctly):

Try
    Dim s As String = "1234".Substring(3, 5) 'some code that throws an exception
Catch ex As Exception
    MsgBox("Error: " + ex.Message + vbCrLf + vbCrLf + "Stack Trace: " + ex.StackTrace, MsgBoxStyle.Exclamation)
End Try
0

精彩评论

暂无评论...
验证码 换一张
取 消