Clearing objects

  • Thread starter Thread starter Ricardo Furtado
  • Start date Start date
R

Ricardo Furtado

for years, in every form i create in VB .Net 2005, in the FormClosing event i
insert the following code:

GC.Collect()
GC.WaitForPendingFinalizers()

but now i'm creating an application that has a very complex form and i
notice that the first time that i use that form in the application the form
opens fast, but after that it opens very slowly. I can only think that the
problem has to do with cleaning the variables/objects. Does this code, that
i've been inserting is the right one to clear objects on a form? is the
correct form to do it or does someone has a better option or can you give me
an hint for code optimization in this aspect?

My thanks in Advanced
 
Hello Ricardo

It shouldn`t be necesary to call the GC at all , so remove this code unless
you have a verry good valid reasson to do so
the GC is optimized for best performance by MS , interfering with it`s
collection mechanism wil mostly hurt your applications performance

HTH

Michel Posseth [MCP]
http://www.vbdotnetcoder.com
 
Ricardo,

I am curious who adviced you to do that, as it is stated to be completely
wrong.

Like as Michel wrote there can be reasons, and therefore you can use it.

But certainly not in the way you do, normally you should not even use the
FormClosing event.
(mostly that event is only used to set something and as well only as it is
really needed something as e.cancel = true)

Cor
 
first of all, thank you both for your answer.

who adviced me was a university teacher that tought us VB .Net and is a C#
programmer.
I use FormClosing in order to clear objects, like this, for instance:
m_grPicDraw = Nothing

This is something that came with me since vb6. I know that .Net should
manage this automaticly, but maybe because of my past experiences with vb
(5/6), i thought this could be a best practice

Best Regards

Ricardo Furtado
 
Hello Ricardo ,


In C# the same general rule applies , that you normally not interfere with
the GC unless you have a valid reasson to do so
normally you call the dispose of objects that implement Idisposable , or
even better write them in a using end using block

In a form the dispose of a control is automaticly called when the dispose of
the form itself is called ( parent child structure )
Code objects however should be disposed of by calling there dispose method
( eg check if a object implements idisposable )

Setting a pointer to nothing by itself does generally do nothing however
Close , Dispose , Nothing may result in faster cleanup by the GC

HTH

Michel Posseth [MCP]
 
Ricardo Furtado said:
who adviced me was a university teacher that tought us VB .Net and is a C#
programmer.
I use FormClosing in order to clear objects, like this, for instance:
m_grPicDraw = Nothing

This is something that came with me since vb6. I know that .Net should
manage this automaticly, but maybe because of my past experiences with vb
(5/6), i thought this could be a best practice

Setting local variables to 'Nothing' at the end of a procedure is almost as
useless as setting private variables to 'Nothing' when closing a form. I
suggest to read the chapters about Garbage Collection in the MSDN Library.
..NET doesn't use reference counting for cleanup any more (as COM and Classic
VB did).
 
first of all, thank you both for your answer.

who adviced me was a university teacher that tought us VB .Net and is a C#
programmer.
I use FormClosing in order to clear objects, like this, for instance:
m_grPicDraw = Nothing

This is something that came with me since vb6. I know that .Net should
manage this automaticly, but maybe because of my past experiences with vb
(5/6), i thought this could be a best practice

Best Regards

Ricardo Furtado

Please realize that being a professor doesn't mean they have a clue
what they're talking about :-)

As the other posters have said, very, very rarely is it ever a good
idea to interfere with the Garbage Collector, it's best to just leave
it as is.

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 
Back
Top