GC (programming style question)

  • Thread starter Thread starter Vladimir
  • Start date Start date
V

Vladimir

Hello, All!

When I should call GC.Collect() to force garbage collection?

Regards,
Vladimir.

Winamp 5.0 (not active)
 
Vladimir said:
Hello, All!

When I should call GC.Collect() to force garbage collection?

General rule: Never.

Advanced rule: if, and only if, you release a huge amount of resources(maybe
a very large object tree), calling GC.Collect() *might* help you by
releasing memory faster, but there is no guarentee that it actually will.
 
Vladimir said:
Hello, All!

When I should call GC.Collect() to force garbage collection?

Regards,
Vladimir.

Winamp 5.0 (not active)


When a part of you program is real time. You would first collect, then start
the part(s) that is real time.
 
Daniel said:
General rule: Never.

Advanced rule: if, and only if, you release a huge amount of
resources(maybe a very large object tree), calling GC.Collect()
*might* help you by releasing memory faster, but there is no guarentee
that it actually will.

And of course, you should call

GC.Collect();
GC.WaitForPendingFinilizers();
GC.Collect;

--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp
 
implement the IDispose interface in your class you wanna do the clean up...

and do it in the 'Dispose' method...
 
Dispose is great for cleaning up precious resources in an object, it
is still up to the garbage collector to free unused memory blocks and
compact the heap.

That being said, it is a rare application that actually needs to force
a collection.
 
011 said:
Hello, Daniel!
You wrote on Sat, 13 Mar 2004 01:29:43 -0600:

DOC> General rule: Never.
DOC> Advanced rule: if, and only if, you release a huge amount of
DOC> resources(maybe a very large object tree), calling
DOC> GC.Collect() *might* help you by releasing memory faster,
DOC> but there is no guarentee that it actually will.

I've developed not very complex console app in C# that realizes some math
algorithm with many memory allocations using "new" operator. I have some
custom collections derived from System.Collections.CollectionBase. I
thought that memory would be released when collection object goes out of
scope. But MSDN says that garbage collection process is being initiated
when there is no free memory for allocation.

Well I've decided to place two Console.ReadLine() calls at the very
beginning and two calls at the very end of Main proc (see below) to see
how much memory is used through Task Manager (not using debugging).

static int Main()
{
Console.ReadLine(); // here we have 4480 KB used
<do some math & memory allocation>
Console.ReadLine(); // 5184 KB used
GC.Collect();
Console.ReadLine(); // 5184 KB used (again!)
return 0;
}

Well I'm surprised that at the beginning of execution there is almost 4.5
MB used. Is that a "feature" of managed apps?
And GC does nothing. Why?

The 4.5 megs is basically due to runtime and the like. Remember, the .NET
runtime is rather rich and has to have structures for its heap, etc, as well
sa pre-reserved memory for object allocation.

As for why the GC collect call may not be doing you any good, its
particularly hard to say. Are you using finalizers? If so you should include
a call to GC.WaitForPendingFinalizers() and call GC.Collect() again, as
Jochen suggested in the other thread. This is because when the GC marks a
finalizable object as free to reclaim, it and its *entire* object graph are
promoted until the finalizer has been run, if memory serves. This could
possibly be part of your problem. Another possibility, and the more likely
one, is that even though the GC has deallocated the memory interally, it
hasn't bothered to return memory to the OS. Task manager isn't the best way
to determine memory usage, consider using the .NET performance counters
 
Back
Top