|
-
Jun 12th, 2008, 09:01 AM
#1
Thread Starter
Frenzied Member
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?
-
Jun 12th, 2008, 01:52 PM
#2
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.
-
Jun 12th, 2008, 02:32 PM
#3
Thread Starter
Frenzied Member
Re: Memory Management and the Garbage Collector
 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);
-
Jun 12th, 2008, 02:45 PM
#4
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jun 12th, 2008, 03:35 PM
#5
Thread Starter
Frenzied Member
Re: Memory Management and the Garbage Collector
 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.
-
Jun 12th, 2008, 04:05 PM
#6
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.
-
Jun 12th, 2008, 04:06 PM
#7
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.
-
Jun 12th, 2008, 04:17 PM
#8
Thread Starter
Frenzied Member
Re: Memory Management and the Garbage Collector
 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?
-
Jun 12th, 2008, 04:33 PM
#9
Re: Memory Management and the Garbage Collector
 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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jun 14th, 2008, 11:46 AM
#10
Fanatic Member
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
-
Jun 15th, 2008, 03:04 AM
#11
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|