Reputation: 1083
I have two forms "Main" and "Clients". Application starts with Main and it contains a button that opens the second one. But when I close Clients, Process Memory (Visual Studio diagnostic tools) don't reduce and if I open the form again it raises (like it creates another instance without destroying previous one). I've created the destructor method just to see if it's called but it didn't.
public partial class MainMenu : Form {
public MainMenu() {
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e) {
//Data obtained from API
List<Client> clientList = new List<Client> {
new Client(0, 0, "Ralph", "asd", "asd", "asd", "asd", "asd", "asd", "asd", "asd")
};
//I bind this list to BindingSource of Client form
BindingListView<Client> cL = new BindingListView<Client>(clientList);
//Client form
BaseForm<Client> form = new BaseForm<Client>(new UI.Content.ClientContent(), cL, Measures.ClientsFormSize);
form.Show();
}
}
This is for Client form:
public partial class BaseForm<T> : Form {
public class SearchItem {
private string name;
private string value;
public string Name { get => name; }
public string Value { get => value; }
public SearchItem(string name, string value) {
this.name = name;
this.value = value;
}
public override string ToString() {
return name;
}
}
private UserStatus currentStatus;
private Forms.BaseContent formContent;
private List<ICustomControl> controlList;
private int registers;
internal BindingListView<T> Data {
get => (BindingListView<T>)formContent.BindSource.List;
set {
formContent.BindSource.DataSource = value;
}
}
internal BaseForm(Forms.BaseContent content, BindingListView<T> data, Size formSize) : this(content, data) {
Size = formSize;
}
internal BaseForm(Forms.BaseContent content, BindingListView<T> data) : this(content) {
Data = data;
registers = Data.Count;
Last();
}
internal BaseForm(Forms.BaseContent formContent) : this() {
this.formContent = formContent;
FitContentToPanel();
}
internal BaseForm() {
InitializeComponent();
currentStatus = UserStatus.View;
}
}
I think this is due to form
is never assigned to null and for that the GC won't take it. I've tried doing a static member and override the OnClosingForm
to prevent closing but I think there are a better way.
Upvotes: 0
Views: 963
Reputation: 849
GC is not supposed to reclaim memory right away after you close a child form.
Try forcing collection by calling .Collect()
2-3 times before you open the child form and after the form is closed. If the memory is not released that would mean you've got a memory leak.
GC.Collect();
GC.Collect();
GC.Collect();
To clarify, I suggest forcing GC to collect memory only as a quick and dirty way to check the memory in Debug time. Do not check-in such code, nor include it into release version.
Upvotes: 1
Reputation: 6563
Use this syntax for faster memory disposal:
using (YourForm f = new YourForm())
{
// ...
f.ShowDialog();
}
Upvotes: 2