Results 1 to 11 of 11

Thread: Memory Management and the Garbage Collector

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Question Memory Management and the Garbage Collector

    A garbage collector, in theory, can reclaim memory right after it goes out of scope.

    Forms, however, seem to be a rare exception where they can exist beyond the scope they're created in.

    For example, in an application you have two simple forms, Form1 and Form2.

    A button click on Form1 creates an instance of Form2 and displays the form.

    Code:
    public partial class Form1 : Form
    {
    	private void button1_Click(object sender, EventArgs e)
    	{
    		Form2 frm2 = new Form2();
    		frm2.Show();
    
    	}
    }
    After button1_Click, frm2's scope is over, yet it still exists in memory and apparently hasn't been GCed yet.

    How can this be? Does the GC make exceptions in special cases?

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Memory Management and the Garbage Collector

    Isn't the Form2 object still being referenced by the fact that it's open? It's owned by Form1.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Re: Memory Management and the Garbage Collector

    Quote Originally Posted by mendhak
    Isn't the Form2 object still being referenced by the fact that it's open? It's owned by Form1.
    Absolutely not.

    This can be verified:
    Code:
    private void Form2_Load(object sender, EventArgs e)
    {
    	Form1 owner = this.Owner as Form1;
    	// owner == null
    }

    The only way to make Form1 own the Form2 instance is:
    Code:
    Form2 frm2 = new Form2();
    frm2.Owner = this;
    frm2.Show();
    or

    Code:
    Form2 frm2 = new Form2();
    
    frm2.ShowDialog(this);

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Memory Management and the Garbage Collector

    The garbage collector does not run instantly and usually is not a preferred function to call in code. Are you .Close or .Dispose your objects too? If so that will mark them for a gc cleanup.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Re: Memory Management and the Garbage Collector

    Quote Originally Posted by RobDog888
    The garbage collector does not run instantly and usually is not a preferred function to call in code. Are you .Close or .Dispose your objects too? If so that will mark them for a gc cleanup.
    Garbage Collection is not an option, it will happen sooner or later regardless whether you dispose.

    Consider any simple memory allocation:
    Code:
    private void button1_Click(object sender, EventArgs e)
    {
    	int x = new int();
    }
    Couldn't x theoretically be reclaimed right after button1_Click? It's out of scope at that point.

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Memory Management and the Garbage Collector

    It can theoretically be reclaimed right after the click, but the garbage collector will run when it wants to. Even .Dispose() doesn't call the GC immediately, it marks it for disposal for the next time the GC comes by. If the GC comes by and deems the object suitable for disposal, it will.

    Don't know if you've read this yet
    http://msdn.microsoft.com/en-us/library/0xy59wtx.aspx

    References to an object mean that it won't be collected. Alright, so if Form2 isn't owned by Form1, the fact that it exists means that it's still being referenced.

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Memory Management and the Garbage Collector

    Think of GCs as garbage collectors in real life. They show up late, are usually disgruntled and won't pick up your trash unless you dispose of it properly, in the correct bin. Then they go on strike for a while.

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Re: Memory Management and the Garbage Collector

    Quote Originally Posted by mendhak
    References to an object mean that it won't be collected. Alright, so if Form2 isn't owned by Form1, the fact that it exists means that it's still being referenced.
    Could this be because Form2 is still being referenced in the Message Loop?

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Memory Management and the Garbage Collector

    Quote Originally Posted by wey97
    Garbage Collection is not an option, it will happen sooner or later regardless whether you dispose.
    That is what I was saying.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10
    Fanatic Member CodedFire's Avatar
    Join Date
    Aug 2007
    Location
    In Cog Neato
    Posts
    568

    Re: Memory Management and the Garbage Collector

    This always seems to be a big issue. But if your code is planned correctly you should know when and were to call dispose() providing it can be called of course. I wonder is it a memory usage threshold that determines when GC is ran?
    Languages: Visual Basic 05/08, C# 08
    IDE: Express Editions
    Framework: 2.0, 3.0, 3.5


    Lesson 5: Don't take domestic advice from perpetual singles. - Mendhak

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Memory Management and the Garbage Collector

    There are no exceptions where garbage collection is concerned. You say:
    A garbage collector, in theory, can reclaim memory right after it goes out of scope.

    Forms, however, seem to be a rare exception where they can exist beyond the scope they're created in.
    but that doesn't really mean anything. Memory doesn't go out of scope. Nor do objects. The only things that can go out of scope are variables. That code creates a Form2 object and assigns it to the frm2 variable. The form is then shown and the frm2 variable goes out of scope. What does that mean? It means that IF that variable was the only managed reference to that object then that object is now eligible for garbage collection. If a form is displayed on-screen then there are definitely more managed references to it that just your variable.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width