Re: Form not being garbage collected
- From: stcheng@xxxxxxxxxxxxxxxxxxxx (Steven Cheng[MSFT])
- Date: Thu, 28 Jul 2005 08:32:53 GMT
Thanks for the followup Joel,
>From the code snippet you pasted, we could say there is no memory leak
prone code in it. I'm not familiar with the two leak detect tools you're
using. As for .NET the memory management are controlled by the runtime and
when a certain object loose all primary reference, it's ready for being
collected, however, there may take some further time for the actual GC
collecting to occur. So I think it's normal that the memory will remain for
some time in .net application(before GC being called by underlying
framework). For a further test, I suggest you try create that subform
multi-times and close it the same times and lookup the VM size( private
bytes) used by the application in system task manager, if there did exist
memory lead, the occupied private bytes will keep increasing untill all the
available ones being exhausted.
Thanks,
Steven Cheng
Microsoft Online Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| From: "Joel Gordon" <joel.gordon@xxxxxxxxxxxxxxxxxxxx>
| Subject: Re: Form not being garbage collected
| References: <#0rDgGkkFHA.3288@xxxxxxxxxxxxxxxxxxxx>
<KHYfYjpkFHA.3120@xxxxxxxxxxxxxxxxxxxxx>
| User-Agent: XanaNews/1.16.5.2
| Message-ID: <eoWViuwkFHA.2644@xxxxxxxxxxxxxxxxxxxx>
| Newsgroups: microsoft.public.dotnet.framework.performance
| Date: Wed, 27 Jul 2005 17:31:01 -0700
| NNTP-Posting-Host: 163.7.4.161
| Lines: 1
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.performance:3415
| X-Tomcat-NG: microsoft.public.dotnet.framework.performance
|
| Hi Steven,
|
| Thanks for the reply. Answers to your questions below :
|
| > app, you found that the main form of your app always not get garbage
| > collected after it is disposed,yes?
| Your description is correct except that the form which is not being
garbage
| collected is not the main form, but a form which is created during the
running
| of the app.
|
| > How did you dispose the form? Just interactively close it or
| programmatcially?
| The form is closed interactively.
|
| > Also, does it behave same if you create a very simple form with a
| > menu/menuitem (event handler in form class)?
| Yes, I have created a trivial test application with a main form and one
| sub-form which is created and shown when a button in the main form is
clicked.
| The sub-form contains a menu with one item in it with the event handler
defined
| in the sub-form.
|
| The profiler reports that this form is still live (i.e. it has not been
garbage
| collected) even though it has been disposed.
|
| I have included the source for the two forms below :
|
| Is this a known problem or am I doing something wrong ?
|
|
| Thanks,
| Joel.
|
| ---- Form1.cs
-------------------------------------------------------------
| using System;
| using System.Drawing;
| using System.Collections;
| using System.ComponentModel;
| using System.Windows.Forms;
| using System.Data;
|
| namespace WindowsApplication1
| {
| /// <summary>
| /// Summary description for Form1.
| /// </summary>
| public class Form1 : System.Windows.Forms.Form
| {
| private System.Windows.Forms.Button button1;
| /// <summary>
| /// Required designer variable.
| /// </summary>
| private System.ComponentModel.Container components = null;
|
| public Form1()
| {
| //
| // Required for Windows Form Designer support
| //
| InitializeComponent();
|
| //
| // TODO: Add any constructor code after InitializeComponent call
| //
| }
|
| /// <summary>
| /// Clean up any resources being used.
| /// </summary>
| protected override void Dispose( bool disposing )
| {
| if( disposing )
| {
| if (components != null)
| {
| components.Dispose();
| }
| }
| base.Dispose( disposing );
| }
|
| #region Windows Form Designer generated code
| /// <summary>
| /// Required method for Designer support - do not modify
| /// the contents of this method with the code editor.
| /// </summary>
| private void InitializeComponent()
| {
| this.button1 = new System.Windows.Forms.Button();
| this.SuspendLayout();
| //
| // button1
| //
| this.button1.Location = new System.Drawing.Point(142, 149);
| this.button1.Name = "button1";
| this.button1.Size = new System.Drawing.Size(191, 33);
| this.button1.TabIndex = 1;
| this.button1.Text = "Run";
| this.button1.Click += new System.EventHandler(this.button1_Click);
| //
| // Form1
| //
| this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
| this.ClientSize = new System.Drawing.Size(512, 376);
| this.Controls.Add(this.button1);
| this.Name = "Form1";
| this.Text = "Form1";
| this.ResumeLayout(false);
|
| }
| #endregion
|
| /// <summary>
| /// The main entry point for the application.
| /// </summary>
| [STAThread]
| static void Main()
| {
| Application.Run(new Form1());
| }
|
| private void button1_Click(object sender, System.EventArgs e) {
| Form2 form = new Form2();
| form.Show();
| }
| }
| }
|
| ---- Form2.cs
-------------------------------------------------------------
|
| using System;
| using System.Drawing;
| using System.Collections;
| using System.ComponentModel;
| using System.Windows.Forms;
|
| namespace WindowsApplication1
| {
| /// <summary>
| /// Summary description for Form2.
| /// </summary>
| public class Form2 : System.Windows.Forms.Form
| {
| private System.Windows.Forms.MainMenu mainMenu1;
| private System.Windows.Forms.MenuItem menuItem1;
| private System.Windows.Forms.MenuItem menuItem2;
| /// <summary>
| /// Required designer variable.
| /// </summary>
| private System.ComponentModel.Container components = null;
|
| public Form2()
| {
| //
| // Required for Windows Form Designer support
| //
| InitializeComponent();
|
| //
| // TODO: Add any constructor code after InitializeComponent call
| //
| }
|
| /// <summary>
| /// Clean up any resources being used.
| /// </summary>
| protected override void Dispose( bool disposing )
| {
| if( disposing )
| {
| if(components != null)
| {
| components.Dispose();
| }
| }
| base.Dispose( disposing );
| }
|
| #region Windows Form Designer generated code
| /// <summary>
| /// Required method for Designer support - do not modify
| /// the contents of this method with the code editor.
| /// </summary>
| private void InitializeComponent()
| {
| this.mainMenu1 = new System.Windows.Forms.MainMenu();
| this.menuItem1 = new System.Windows.Forms.MenuItem();
| this.menuItem2 = new System.Windows.Forms.MenuItem();
| //
| // mainMenu1
| //
| this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
| this.menuItem1});
| //
| // menuItem1
| //
| this.menuItem1.Index = 0;
| this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
| this.menuItem2});
| this.menuItem1.Text = "Test Menu";
| //
| // menuItem2
| //
| this.menuItem2.Index = 0;
| this.menuItem2.Text = "THIS IS THE TEST MENU";
| this.menuItem2.Click += new
System.EventHandler(this.menuItem2_Click_1);
| //
| // Form2
| //
| this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
| this.ClientSize = new System.Drawing.Size(292, 273);
| this.Menu = this.mainMenu1;
| this.Name = "Form2";
| this.Text = "Form2";
|
| }
| #endregion
|
| private void menuItem2_Click_1(object sender, System.EventArgs e) {
| MessageBox.Show("Testing menuItem2_Click()");
| }
| }
| }
|
|
|
.
- References:
- Form not being garbage collected
- From: Joel Gordon
- RE: Form not being garbage collected
- From: Steven Cheng[MSFT]
- Re: Form not being garbage collected
- From: Joel Gordon
- Form not being garbage collected
- Prev by Date: RE: Few C# Questions
- Next by Date: Re: Bitmaps vs threading
- Previous by thread: Re: Form not being garbage collected
- Next by thread: Application Center Test - test gives better results with more brow
- Index(es):
Relevant Pages
|