locked
GC RRS feed

  • Question

  • Hi,

    GC "collects" memory when there is no reference in code to it. (Actually not really it is so, yet for simplicity reasons I've written so) Is there a way to get number of actual code references to an object, asuming that I have at least one. I guess GC might know such information, yet I was unable to get to right method. The weakreference isn't the resolution...

     

    Greethings

     

    Friday, December 29, 2006 2:18 PM

Answers

  • Under a native debugger, you can load sos and use the gcroot command.

    A programattic approach is a little more difficult. You'll need to use C++ and the CLR profiling API to get this type of information.

    Saturday, December 30, 2006 3:07 AM

All replies

  • This appears well hidden from prying eyes.  I see nothing in the native debugger and profiler interfaces either.  This is possibly due to the Heisenberg Principle; you cannot watch the GC heap without affecting the way it works.
    Friday, December 29, 2006 3:32 PM
  • The GC doesn't keep a running tally of reference counts.  When a collection occurs the GC attempts to "backtrack" each reference to see if it is still rooted.  It doesn't "count" how many their are, it only case that it is still needed then continues looking for references to collect...

    Since you asked the question, why do you think you need to know the reference count of an object?
    Friday, December 29, 2006 4:34 PM
  • Under a native debugger, you can load sos and use the gcroot command.

    A programattic approach is a little more difficult. You'll need to use C++ and the CLR profiling API to get this type of information.

    Saturday, December 30, 2006 3:07 AM
  • Thanks very much Scott!

    Explanation for Peter: Reference counter can be useful in making (and tuning) own  cache mechanizm, and for tracking references that should be nullptr-s yet they aren't, espetially in multithread environment.

    Saturday, December 30, 2006 5:39 PM