I have a Winform application that I am using with WCF and SQL. Part of the program that the users use to create a report will dynamically create custom user controls and place a reference to them into a hashtable that I maintain. Then as the user navigates through the report by selecting nodes on a treeview, I load and unload the User Controls into a panel.
Here is the code I use to create them and place them into the Collection
Here is the code that moves them in and out of the Panel on the form as the user navigates through the treeview
And here is the code I use in the forms ?Closing? event to try to clean up the memory when the User closes this case. I?ve done it with and without the ?.Dispose()? line and the ?= Nothing? line. This code does get executed but doesn?t seem to help.
When I go into a memory profiler, the controls stay in memory even though the form that created them has been unloaded from memory. The profiler reports that the control listed above has held memory of 8818k. When a report is closed and another is opened, the held memory for this control increases by 8818k. And continues to increase by that amount for each report that is opened. As you can imagine, people opening and closing a lot of reports eventually get an out of memory exception.
Here is the code I use to create them and place them into the Collection
VB.NET:
ucIncidentInstance = New ucIncidentMR(IncidentReport, IncidentReportGUID)
ucIncidentInstance.Tag = "IncidentMR"
ucIncidentInstance.Dock = DockStyle.Fill
Globals.ControlTable.Add(IncidentReportGUID, ucIncidentInstance)
Globals.GlobalNode.Nodes.Add(IncidentReportGUID, "Incident Report")
Here is the code that moves them in and out of the Panel on the form as the user navigates through the treeview
VB.NET:
If Globals.ControlTable.ContainsKey(e.Node.Name) Then
panelMain.Controls.Clear()
panelMain.Controls.Add(CType(Globals.ControlTable(e.Node.Name), UserControl))
Else
If e.Node.Name = "MainNode" Then
panelMain.Controls.Clear()
End If
End If
And here is the code I use in the forms ?Closing? event to try to clean up the memory when the User closes this case. I?ve done it with and without the ?.Dispose()? line and the ?= Nothing? line. This code does get executed but doesn?t seem to help.
VB.NET:
For Each item As DictionaryEntry In Globals.ControlTable
If item.Key = IncidentReportGUID Then
Dim myControl As ucIncidentMR
myControl = CType(Globals.ControlTable(item.Key), ucIncidentMR)
myControl.Dispose()
myControl = Nothing
End If
Next
When I go into a memory profiler, the controls stay in memory even though the form that created them has been unloaded from memory. The profiler reports that the control listed above has held memory of 8818k. When a report is closed and another is opened, the held memory for this control increases by 8818k. And continues to increase by that amount for each report that is opened. As you can imagine, people opening and closing a lot of reports eventually get an out of memory exception.