-4

I have many user controls where I add only one of them each in a panel but first clear the panel from the previous one.

I create a new instance of the user control when I want to add it in the panel, and Dispose() the other user controls and set all the references to null to allow the garbage collector to delete them.

For example:

// Declare the variables globally
ViewBasicInformation control1 = null;
AddBasicInformation control2 = null;

// Code inside Button
Panel.Controls.Clear(); 
control1.Dispose(); 
control1 = null;
control2 = new AddBasicInformation();  
Panel.Controls.Add(control2);

However my memory usage keeps increasing, how do I release this memory?

And for more information ... Every user control consumes a class that makes a connection to a smart card and executes some commands to read and write to the card.

I also have a backgroundworker that detects when the card is inserted or ejected.

Kev
  • 118,037
  • 53
  • 300
  • 385
Dark_Knight
  • 387
  • 4
  • 20

2 Answers2

3

The .NET runtime uses garbage collection - I urge you not to manage memory yourself.

I would expect memory to go up initially - if it never goes down, you have dangling references somewhere. Chances are you are subscribing to events and when the objects that have been subscribed are no longer needed you do not unsubscribe from these events. This is the most common cause of memory leaks in .NET applications (though dealing with unmanaged code is a close second).

Make sure you unsubscribe from any control event that you have subscribed to before you clear out the reference to it.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • If i understand you right, you mean that i add events in runtime? No, my events is added in the design time .... do i have to write code to remove them ? – Dark_Knight Aug 15 '11 at 10:21
  • @Sayed - I am saying that is the most common cause. I can't tell from your code one way or another. – Oded Aug 15 '11 at 10:21
  • All the events are added in the design mode .. do i need to write code to remove them? – Dark_Knight Aug 15 '11 at 10:45
  • @Sayed: No, if the events are added at design time you do *not* need to remove them manually. – Cody Gray - on strike Aug 15 '11 at 10:49
  • What exactly convinces you that there *is* a problem? – Cody Gray - on strike Aug 15 '11 at 12:40
  • the memory usage is increasing while i add new usercontrol or open new form and doesn't decrease when i close this form or clear the panel from the user control. – Dark_Knight Aug 15 '11 at 13:00
  • @Sayed - did you try clearing out the controls recursively? Using `Controls.Clear` is not a good way to do things - you should iterate though each controls collection recursively and call `Dispose` on each control. – Oded Aug 15 '11 at 13:02
  • But the Panel is cleared already after i called Panel.Controls.Clear(); – Dark_Knight Aug 15 '11 at 13:11
  • @Sayed - Which is the wrong thing to do. You are not disposing of the controls that are held by `Panel.Controls`. – Oded Aug 15 '11 at 13:16
  • @Sayed - What about the controls it holds? An any _other_ controls that were on the panel? – Oded Aug 15 '11 at 13:30
  • @Oded: no, In every time there is only one user control in the panel ... no more – Dark_Knight Aug 15 '11 at 23:00
1

Regardless of the garbage collector being quite clever about when it has to run and when it doesn't need to, there are a number of error sources where you effectively get yourself into memory leaks based on event handling and different lifetimes of objects. This has been a subject on SO before (e.g. here) and I have written a little bit about some mistakes we did on one project that was the cause of memory leaks here: Do you think your Windows.Forms Application has no memory leaks?

Community
  • 1
  • 1
flq
  • 22,247
  • 8
  • 55
  • 77