I built a project where in one form (frmDash) I add several forms as controls in a container.
frmChart.TopLevel = False
frmChart.FormBorderStyle = False
frmChart.ControlBox = False
container.Controls.Add(frmChart)
When I close the frmDash the memory is decreased but not as much as it expected.
So every time that I reopen the frmDash the memory is increased in relation with the previous time that it was opened.
When I close the frmDash :
For each frm as frmChart in container.Controls
frm.Close
Next
container.Controls.Clear
container.Dispose
And in frmChart dispose method I dispose everything that I have (datatables, lists, charts -I know that I shouldn’t dispose some of those , but I am trying to find what is wrong).
Does anyone knows what I am doing wrong?
I found this here:
I also want to mention that, DataSet.Clear() and DataSet.Dispose() won’t make the rows cleared by GC. .Clear() method will remove all the data rows in every datatable inside the dataset. However, it does not remove the tables and relationships. Also, the .Dispose() method is implemented by the base class of the DataSet MarshalByValueComonent, so it does not release the managed resources of dataset.
To let GC clear all the datatable and datarows, please set the ds to null so all the managed resource of the original dataset are not referred and GC can finally clear them all. Another method is using DataSet.Reset, so the dataset will be at the initialized status without any tables or relationships added.
If that is true, I have to set every datatable to nothing because right know I just disposing it.
I did that, and I set every datatable that I used equal to nothing , but the memory leakage remains.
If you create any event handlers to the controls, these also need to be released. .Net’s GC will scan it’s object to look for a link to the object. Calling dispose will stop you from being able to reference the object but if there is an event handler still attached, the GC thinks that the event could still be raised.
In VB.Net you can do
This will then allow the GC to remove the object from memory.