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...
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?
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.
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...
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.
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).
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.