0

The MSDN documentation for GC.CollectionCount states: "Returns the number of times garbage collection has occurred for the specified generation of objects."

Does this mean it only performs this analyzation on the Generational Heap Or Is There Some Way To Measure The Amount Of Collections occurred In The Large Object Heap As well?

  • Is there any reason you need this? Usually you would just use a profiler and not worry about these sorts of methods unless you are building something very specific – TheGeneral Jan 18 '21 at 03:33
  • Also same question with GC.Collect does it only perform its actions on the Generational Heap since MSDN also states: "Forces an immediate garbage collection of all generations". I might be taking the statement(s) to literally but just cautious to make sure I'm not confusing things. – JohnnyStant Jan 18 '21 at 03:35
  • Not really its just for curiosity – JohnnyStant Jan 18 '21 at 03:35
  • In regards to GC.Collect it performs a collection on all generations, which includes generation 3 (aka the large object heap) – TheGeneral Jan 18 '21 at 03:38
  • in regards to `GC.CollectionCount(Int32)` you specify the generation, so if you target 3, you will get the LOH – TheGeneral Jan 18 '21 at 03:39
  • Take a look at this https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/fundamentals#generations – TheGeneral Jan 18 '21 at 03:40
  • Oh thankyou that answered my question – JohnnyStant Jan 18 '21 at 03:40
  • What do you mean by the `Generational Heap`? – mjwills Jan 18 '21 at 03:52
  • Also be aware of https://learn.microsoft.com/en-us/dotnet/api/system.runtime.gcsettings.largeobjectheapcompactionmode?view=net-5.0 if you are interested in the LOH. – mjwills Jan 18 '21 at 03:53
  • Generation Heap just being the small object heap (gen0/1/2) – JohnnyStant Jan 18 '21 at 03:58

1 Answers1

1

You really shouldn't be too worried about these types of methods unless you are building something very specific or have a targeted use case. Usually, you would get this information from a the result of a memory profiler which will give you much more useful information

Some relevant information

Generations

Newly allocated objects form a new generation of objects and are implicitly generation 0 collections. However, if they are large objects, they go on the large object heap (LOH), which is sometimes referred to as generation 3. Generation 3 is a physical generation that's logically collected as part of generation 2.

With GC.CollectionCount it takes a generation integer, so you can specify 3 if you like

In regards to GC.Collect this will do a full collect of all generations.

There is obviously a lot more to this topic which has been glossed over. However, the following is a good place to start

Fundamentals of garbage collection

TheGeneral
  • 79,002
  • 9
  • 103
  • 141