MVP's Please respond!

  • Thread starter Thread starter Vai2000
  • Start date Start date
V

Vai2000

Hi All, To my correct understanding Garbage Collection works based on
Generation algorithm...(high level).
I have an application which does some heavy operations usually File I/O's. I
want to force garbage collection on this class. I still want the GC to
release all resources before finalizing etc...but I want GC to kick in ASAP,
rather waiting to execute its algo to detect that this class needs cleaning
up and finally come to its rescue...

This is what I have implemented. Please advice

TIA

class Foo:IDisposable
{

public void Dispose()
{
GC.Collect();
}

}
 
[For future reference - it would be better to give a subject line which
actually describes your problem. You may also have alienated others who
have a perfectly good answer but aren't MVPs.]

Vai2000 said:
Hi All, To my correct understanding Garbage Collection works based on
Generation algorithm...(high level).
I have an application which does some heavy operations usually File I/O's. I
want to force garbage collection on this class. I still want the GC to
release all resources before finalizing etc...but I want GC to kick in ASAP,
rather waiting to execute its algo to detect that this class needs cleaning
up and finally come to its rescue...

This is what I have implemented. Please advice

class Foo:IDisposable
{
public void Dispose()
{
GC.Collect();
}
}

That looks like a very bad idea to me. Why do you think it's a good
idea to force garbage collection? Much better would be to call Dispose
on all the appropriate streams explicitly. That will release the
unmanaged resources but not force a potentially expensive garbage
collection.
 
Hi Vai2000 ,

Take a look at System.GC class. It has methods:
GC.Collect() to force grabage collection
GC.WaitForPandingFinalizers() - after calling GC.Collect it will block until
all finalizers finish.
 
Back
Top