Re: Form not being garbage collected

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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()");
| }
| }
| }
|
|
|

.



Relevant Pages

  • Re: Real time computing
    ... > Yes, I understand, but I expect the GC to not bluntly collect garbage until ... > pull my leash, I expect it to be very very gentle with me, only collecting ... > common sense, speculation and wishfull thinking on my part so I would be ... memory, ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Garbage collection
    ... I've read a few threads in this group recently, discussing garbage ... collecting and unsetting variables and the likes. ... Concerning memory usage ... Would it be good practice for instance to unset or set variables to NULL ...
    (comp.lang.php)
  • Garbage collection
    ... I've read a few threads in this group recently, discussing garbage ... collecting and unsetting variables and the likes. ... Would it be good practice for instance to unset or set variables to NULL ... from memory when out of scope? ...
    (comp.lang.php)
  • Re: Question about return by reference?
    ... Definition of memory leak: ... "The effect of a program that maintains references to objects that ... Just to complete Doug's excellent answer: There is no Garbage Collector ... definition for memory leak in C and C++ would be (out of the top of my ...
    (microsoft.public.vc.language)
  • Re: Queue cleanup
    ... without ever touching the garbage, and then they release the old heap. ... And suddenly you’ve doubled the memory requirements. ... that‘s not how locality of reference works. ...
    (comp.lang.python)