siva
siva

Reputation: 1265

System.OutofMemory exception

I have a UI, winform, It has loads of controls like tabcontrols, Plenty of text boxes(min 25 spread across tabs) comboxes, checkboxes, buttons, labels, tree control etc.

most of these controls register for ctrl events like textchanged, click etc... Since i am getting this out of memory exception and all i have few queries:

Questions:

  1. In the dispose function of the form should dispose of each of this ctrl be called ?
  2. Should the events subscibed by the ctrls like the textchanged, click event etc be de-registered in the dispose function ? Does the GC take care of de registering the events added for the ctrls, as the code snippets are added in the designer?

Some Info: The application was put on a stability test for upto 100 iterations on a 2GB ram machine. Each iteration it would execute the same test(launching teh dialog, doing some clicks or editing some values, and closing). It was on running for almost 8 hrs. on the 45 th or the 46 th iteration it threw this exception. Yes all the controls in the dialog have one event or ther other registered. some text changed some checkedchanged or click etc

Upvotes: 0

Views: 252

Answers (1)

Adam Lear
Adam Lear

Reputation: 38758

I don't think there's enough information here to tell you exactly what's going on. This will take some careful analysis of the code on your part, but the first thing to do is to run your app through a memory profiler. For example, ANTS Memory Profiler, but really anything you have access to will do fine) to see what's really going on.

Normally, you wouldn't need to explicitly dispose of every control, but if you're subscribing to events, that can cause things to hang around in memory longer than usual. This question has the gory details, but in a nutshell, you want to unsubscribe or use weak references to fix that; no need to enumerate all form controls and call Dispose. If I recall, it should be called on Form.Close() anyway, unless it's an MDI form and it's not visible when Close is called.

You should also look at what data you're storing and how, as well as whether or not you're properly disposing of whatever resources your application may be using.

If you're running on a 32-bit system, you might also be running against the per-process memory limit of 2GB, which you can hit if you, say, keep a List around that grows to 1GB and has to resize.

So, arm yourself with a profiler and get investigating. :)

Upvotes: 1

Related Questions