I have a problem with a memory leak in a .NET CF application.
Using RPM I identified that dynamically creating controls are not garbage collected as expected. Running the same piece of code in .NET Window Forms behave differently and disposes the control as I expected.
See the output from RPM via PerfMon for the Process Heap counter:

GC Heap:

My best guess is that the Weak Reference to the Panel is for some unknown reason not making the object eligible for GC, can it be?
Please note: Even though Dispose() solves the problem for the sample, I can’t easily incorporate it into the existing application as it is not as clear cut to determine when the object is no longer in use.
I have included a simplified version of the source to illustrate the problem:
using System; using System.Windows.Forms; namespace CFMemTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } // Calling this event handler multiple times causes the memory leak private void Button1_Click(object sender, EventArgs e) { Panel uc = new Panel(); // Calling uc.Dispose() cleans up the object } } }
Update:
1. Calling GC.Collect() also doesn’t result in the panels being cleaned up.
2. Using .NET CF 2.0 SP1 on a Windows CE 4.2 device.
Some additional information here that explains this behaviour.
According to Ilya Tumanov: