Paul.Lee.1971 wrote:
Hi everyone,
A program that I'm helping to code seems to slow down drastically
during initialisation, and looking at the profiling graph, it seems to
be the garbage collector thats slowing things down. I must point out
that a heck of a lot of data are being read in and manipulated, and I
was wondering if there were any metrics to show much of a performance
hit is occurring during the initialisation? If it turns out that the
GC is having a bad effect on the program, is there a way to suppress
the operation of the GC (although this does sound like a very bad idea
IMHO)
TIA
Paul
--
http://www.paullee.com
Going through a lot of object doesn't autoamtically mean that the
garbage collector has to do a lot of work. What matters is the life span
of the objects.
The garbage collector works by the theory that most objects are very
short lived. All new objects (smaller than 85 kB) are allocated in heap
generation 0, and this is the heap generation that is most often
collected. Collecting a heap genretation is done by moving all used
objects to the next generation and clear out the entire generation.
This means that if you create a lot of object but release them before
the next garbage collection, reclaiming the memory is very cheap. The
garbage collector actually has to do absolute nothing to the objects to
reclaim the memory. When the used objects are removed from the heap
generation, the allocation pointer is reset to the start of the heap
generation, and all the unused objects in it are gone with one single
operaton. Poof.
If you use objects that implement the IDisposable interface, you should
be careful to always call the Dispose method when you are done with
them. Objects that implement IDisposable has a finalizer as backup for
the Dispose method, which means that they are references from the
finalizer queue.
If you call Dispose, the object is removed from the finalizer queue, and
can easily be collected just like any other object. If you don't call
Dispose, the garbage collector has to call the finalizer before the
object can be collected, but calling the finalizer of a lot of objects
takes far too long to do in a garbage collection, so the objects has to
be moved to the next heap generation and wait there for a background
thread to call the finalizer. That means that any disposable object that
is not disposed properly will need at least two garbage collections to
go away.
So, have a look at the life span of the objects you use, and make sure
to dispose disposable objects.
--
Göran Andersson
_____
http://www.guffa.com