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?
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.
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);
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.
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.
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.
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.
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?
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. ;)
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?
Re: Memory Management and the Garbage Collector
There are no exceptions where garbage collection is concerned. You say:
Quote:
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.