public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var f = new UserControl1() { AllowDrop = false};
this.Controls.Add(f);
this.Controls.Remove(f);
GCCheck();
}
private void button2_Click(object sender, EventArgs e)
{
var f = new UserControl1() { AllowDrop = true};
this.Controls.Add(f);
this.Controls.Remove(f);
GCCheck();
}
private void GCCheck()
{
var a = GC.GetTotalMemory(false);
var b = GC.GetTotalMemory(true);
Console.WriteLine("GC Before=" + a + " After=" + b);
}
}
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
~UserControl1()
{
Console.WriteLine(this.GetType().Name + "は解放されました " + "{AllowDrop = " + this.AllowDrop + " }");
}
}
CiAgICBwdWJsaWMgcGFydGlhbCBjbGFzcyBGb3JtMSA6IEZvcm0KICAgIHsKICAgICAgICBwdWJsaWMgRm9ybTEoKQogICAgICAgIHsKICAgICAgICAgICAgSW5pdGlhbGl6ZUNvbXBvbmVudCgpOwogICAgICAgIH0KCiAgICAgICAgcHJpdmF0ZSB2b2lkIGJ1dHRvbjFfQ2xpY2sob2JqZWN0IHNlbmRlciwgRXZlbnRBcmdzIGUpCiAgICAgICAgewogICAgICAgICAgICB2YXIgZiA9IG5ldyBVc2VyQ29udHJvbDEoKSB7IEFsbG93RHJvcCA9IGZhbHNlfTsKICAgICAgICAgICAgdGhpcy5Db250cm9scy5BZGQoZik7CiAgICAgICAgICAgIHRoaXMuQ29udHJvbHMuUmVtb3ZlKGYpOwogICAgICAgICAgICBHQ0NoZWNrKCk7CiAgICAgICAgfQoKICAgICAgICBwcml2YXRlIHZvaWQgYnV0dG9uMl9DbGljayhvYmplY3Qgc2VuZGVyLCBFdmVudEFyZ3MgZSkKICAgICAgICB7CiAgICAgICAgICAgIHZhciBmID0gbmV3IFVzZXJDb250cm9sMSgpIHsgQWxsb3dEcm9wID0gdHJ1ZX07CiAgICAgICAgICAgIHRoaXMuQ29udHJvbHMuQWRkKGYpOwogICAgICAgICAgICB0aGlzLkNvbnRyb2xzLlJlbW92ZShmKTsKICAgICAgICAgICAgR0NDaGVjaygpOwogICAgICAgIH0KICAgICAgICBwcml2YXRlIHZvaWQgR0NDaGVjaygpCiAgICAgICAgewogICAgICAgICAgICB2YXIgYSA9IEdDLkdldFRvdGFsTWVtb3J5KGZhbHNlKTsKICAgICAgICAgICAgdmFyIGIgPSBHQy5HZXRUb3RhbE1lbW9yeSh0cnVlKTsKICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUoIkdDIEJlZm9yZT0iICsgYSArICIgQWZ0ZXI9IiArIGIpOwogICAgICAgIH0KICAgIH0KCgpwdWJsaWMgcGFydGlhbCBjbGFzcyBVc2VyQ29udHJvbDEgOiBVc2VyQ29udHJvbAogICAgewogICAgICAgIHB1YmxpYyBVc2VyQ29udHJvbDEoKQogICAgICAgIHsKICAgICAgICAgICAgSW5pdGlhbGl6ZUNvbXBvbmVudCgpOwogICAgICAgIH0KICAgICAgICB+VXNlckNvbnRyb2wxKCkKICAgICAgICB7CiAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lKHRoaXMuR2V0VHlwZSgpLk5hbWUgKyAi44Gv6Kej5pS+44GV44KM44G+44GX44GfICIgKyAie0FsbG93RHJvcCA9ICIgKyB0aGlzLkFsbG93RHJvcCArICIgfSIpOwogICAgICAgIH0KICAgIH0=