Re: GC.Collect and Generations

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




My understanding is...

GC.Collect() isn't special. It attempts to collect, and any remaining
objects will be upped into the next generation.

There aren't many cases when calling GC.Collect() is a good idea. Doing
so inside a loop sounds odd.

By the way, in your example code, the 'o' object will be collected on
the first call since it's no longer rooted... So it doesn't move up the
genreations..


In article <uSKQshgtGHA.1288@xxxxxxxxxxxxxxxxxxxx>, "Günter Prossliner"
<g.prossliner/gmx/at> says...
Hi everybody!

As we all know, die Garbage Collector minimizes the time needed for a
collection by using generations. When an object remains uncollected, it
moves one generation up. Higher generations are not collected such often as
lower generations. When an object lives in the generation 2 (the highest in
the current implementation), garbage collection will - under most
circumstances - not happen (this is not documented, but it was pointed out
in some MSDN articles).

Ok.

What if the Developer calls GC.Collect()? I have seen code (not mine), where
GC.Collection was called even within loops! From my understanding this would
result in having object in high generations allthough they are not long
living (maybe just Objects that have a liftime within a simple method). This
would result in a very high memory foodprint, since this objects will not
(or very seldom) get collected.

Or does the GC handle calls to GC.Collect() special? I meen that objects are
not moved into a higher Generation?


c# sample:

void foo(){
SmallMethod();
GC.Collect(); // o will move in gen #1
GC.Collect(); // o will move in gen #2
}

void SmallMethod(){
VeryBigManagedObject o = new VeryBigManagedObject();
}


br GP



.



Relevant Pages

  • Re: GC.Collect and Generations
    ... die Garbage Collector minimizes the time needed for a ... Higher generations are not collected such often ... this would result in having object in high generations allthough they are ... void SmallMethod{ ...
    (microsoft.public.dotnet.framework.clr)
  • Re: GC.Collect and Generations
    ... calling GC.Collect directly is a HandleCollector: ... Higher generations are not collected such often ... | result in having object in high generations allthough they are not long ... | void SmallMethod{ ...
    (microsoft.public.dotnet.framework.clr)
  • GC.Collect and Generations
    ... die Garbage Collector minimizes the time needed for a ... Higher generations are not collected such often as ... result in having object in high generations allthough they are not long ... void SmallMethod{ ...
    (microsoft.public.dotnet.framework.clr)
  • Re: Test a class...beginner question
    ... > void MakeTree; ... > int PushBranch; ... which is why understanding the code gives ... that with code I download if it's been available for a while, ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Interview Questions
    ... understanding of design patterns (atleast they should have heard of the ... What is a pure virtual function, and why would you use it? ... class Super1: public Sub ... void DoSomething ...
    (microsoft.public.vc.mfc)