Instances are return cleanly by Garbage Collection mechanism in c#?

  • Thread starter Thread starter E-John
  • Start date Start date
E

E-John

Guest
Dear All,


I don't quite understand how to debug(view) the resource managed by Garbage Collection in C# world.

Here is my test code,

I used an object reference formSub in my code,

And there will be three different Form instances be created when the corresponding radiobutton is checked.

for example,

FormSub1 is created when radioButton1 is checked and it is assigned to formSub reference

formSub = new FormSub1();

The Sub Form is shown by pressed the button [Create]


Because every time the radiobutton is checked, there is a new instance created by new FormSub1 or FormSub2 or FormSub3

The more times I make radiobutton checked, the more sub Forms are created? is there any potential too many instances in my application?

how to make sure it is collected cleanly by Garbage Collection mechanism in c#?

It is heard that create by the following code snippet could make sure the resource is return cleanly by GC mechanism of c#

How to view the resource created and release in Visual Studio Community 2017?

using (var form = formSub)
{
form.ShowDialog();
}

Thanks and Best regards,

E-John

namespace TestWinFormObjectAndInstance
{
public partial class Form1 : Form
{
Form formSub;

public Form1()
{
InitializeComponent();
}

private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
formSub = new FormSub1();
}

private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
formSub = new FormSub2();
}

private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
formSub = new FormSub3();
}

private void buttonCreateSubForm_Click(object sender, EventArgs e)
{
using (var form = formSub)
{
form.ShowDialog();
}
}
}
}



1393772.png

Continue reading...
 
Back
Top