why doesn't GC release the form's memory?

  • Thread starter Thread starter **Developer**
  • Start date Start date
D

**Developer**

Private Sub KeyboardShortcuts()

Dim ff As New FormHelp

ff.Show()

End Sub

End Class



Seems to me that ff is on the stack (?maybe not) and references the instance
of FormHelp. After the sub exits ff nolonger exists and nothing references
the form so why doesn't GC release the form's memory?

Even if ff is not on the stack after the sub exits ff should be available
for GC. No?

Thank
 
The garbage collector is tricky. To get it to clean up an object, you
should call its Dispose method. This flags it for cleanup by the GC. If
an object does not have a dispose method, it is a good idea to remove
the object from memory wherever it exists. For example, if ff is in a
collection, a variable, and an array, you must remove it from the
collection, set the variable to nothing, and set the item in the array
to nothing as well.
 
How do you know that the GC does not remove the form from memory? The GC does
a collection only when there is some kind of memory pressure. If not,
collections run at a lower frequency. The form will be collected when the
next collection runs.

Also if an object has a dispose method, it is always preferable that you
call it so that unmanaged resources are cleaned up fast.
 
Seems to me that ff is on the stack (?maybe not) and references the instance
of FormHelp. After the sub exits ff nolonger exists and nothing references
the form so why doesn't GC release the form's memory?

Winforms hold internal references to all Form objects to prevent them
from being garbage collected before the window closes.




Mattias
 
The garbage collector is tricky. To get it to clean up an object, you
should call its Dispose method. This flags it for cleanup by the GC.

No it doesn't, the GC has no knowledge about the Dispose pattern.




Mattias
 
Thanks - that makes much sense



Mattias Sjögren said:
Winforms hold internal references to all Form objects to prevent them
from being garbage collected before the window closes.




Mattias
 
What are you using? Because it obviously can't be .Net. The Dispose
method of an object tells the object to internally clean up and then to
signal the GC to remove it. In order not to flame but to benefit the
community as a whole, look at this
http://www.devcity.net/Articles/93/1/gc_manage.aspx (a bit of an
explanation on disposing objects).
 
Matt said:
What are you using? Because it obviously can't be .Net. The Dispose method
of an object tells the object to internally clean up and then to signal the
GC to remove it. In order not to flame but to benefit the community as a
whole, look at this http://www.devcity.net/Articles/93/1/gc_manage.aspx (a
bit of an explanation on disposing objects).

Finalization <> Dispose pattern. The GC will call the finalizer, but it
won't call 'Dispose' directly because it doesn't know about this pattern.
However, the finalizer can call the object's 'Dispose' method to make sure
unmanaged resources are released when the object gets destroyed. In other
words: Disposing is not something the GC cares about, it's up to the
developer to implement the pattern correctly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top