calling GC

  • Thread starter Thread starter perspolis
  • Start date Start date
P

perspolis

Hi all
I want to know how can I force GC to be callled?

thanks in advance
 
GC.Collect();

However, this is not recommended in most scenarios. Normally, just let
the GC do its thing. Do you have a specific reason for wanting to
force this? There may be better options...

Marc
 
Hi Perspolis,
you can force the garbage collector to run by calling GC.Collect() which
will force a collection on all generations of eligable objects to be
collected. Generally though you should not have to force the GC since it
knows best when to run, what problem are you trying to solve?

Mark.
 
I have a database application and using pooling in my connection.
In some reasons I want to restore my database (SqlServer).
but because of pooling it give me error "The database is using ";
I have a method that clears SqlConnection pool and place it in destructore.
But when I want tor restore the destructor has no been called yet.
 
Marc said:
GC.Collect();

However, this is not recommended in most scenarios. Normally, just let
the GC do its thing. Do you have a specific reason for wanting to
force this? There may be better options...

Marc

Also
GC.WaitForPendingFinalizers();

If you want a synchronous call to wait until all the finalizers are
complete (the collect method is launched in a separate thread)
 
pedant note: finalizer, not destructor

If you want deterministic cleanup of some bespoke object (of yours),
then IDisposable is a much better pattern than a finalizer.
Theoretically you should just have to call ClearAllPools(), and then
ensure that all in-use connections are properly closed (and ideally
disposed). I haven't tried it though. Of course, since you obviously
have elevated privelege at the database (to invoke a restore), you
could also presumably kill the spids from the server.

http://msdn2.microsoft.com/en-us/library/8xx3tyca(VS.80).aspx

Marc
 
Good catch; in the (undesirable) scenario where you are dependent on a
particular finalizer, then sync rather than async is necessary. But
best to avoid it in the first place (IDisposable etc).

Marc
 

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