![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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 |
#3
| |||
| |||
|
|
What if the Developer calls GC.Collect()? I have seen code (not mine), where GC.Collection was called even within loops! Short answer is *don't* call GC.Collect in a loop. |
|
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 |
#4
| |||
| |||
|
|
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 |
#5
| |||
| |||
|
|
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.. |
#6
| |||
| |||
|
|
Short answer is *don't* call GC.Collect in a loop. |
|
For details on when you should & when you shouldn't (call GC.Collect) see the four web pages on the GC at: http://www.tsbradley.net/Reading/CLR.aspx |
|
Especially read these two: http://blogs.msdn.com/ricom/archive/...29/271829.aspx http://blogs.msdn.com/ricom/archive/.../02/40780.aspx |
#7
| |||
| |||
|
|
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 |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |