How can I know the reference count of an object?

To diagnose memory leak problem, it's convenient to know the "reference count" of an object. Is there any API that could let me I know that e.g.:

object obj = ...
...

int count = GetReferenceCount(obj);
if (count > 0)
{
      Debug.WriteLine(...);
}


Answer this question

How can I know the reference count of an object?

  • BISDamonL

    Hi,

    Unlike COM, the common language runtime does not use reference counting to govern object lifetime. Instead, the garbage collector traces object references and identifies objects that can no longer be accessed by running code.
    So its called Reference Tracing.

    Read the following articles to get a good understanding of Garbage Collection in the .NET Framework:

    Garbage Collection: Automatic Memory Management in the Microsoft .NET Framework

    Garbage Collection—Part 2: Automatic Memory Management in the Microsoft .NET Framework

    If you face issues of memory leaks in .NET then you needto use the CLR Profiler to detect them: CLR Profiler

    Also read: Introduction to CLR Profiler

    Regards,
    Vikram



  • Devast8or

    This is just what I want to know : there is a Reference Tracing, why not expose the result of the tracing In most cases, I don't care the exact number of references, I just want to know if there are some other objects that are still referencing my object. This is very helpful in memory leak detection.



  • sebus

    I have tried to use CLR Profiler (the latest version for .NET Framework 2.0 Beta2), but sometimes when I click "Show heap now", it shows nothing. (I have checked "Profiling active" and "Allocations".

  • Jethro

    I found this will most occur after the program allocated a large mount of memory. In my program (it's a multi-document program, diffent document has different size and different memory cost), when I open a small document, close it (I close it because I want to detect the memory leak during the open-close circle), the CLR Profiler can dump the memory correctly; however, if I open a large document and close it, the CLR Profiler will show a blank window when I click "Show heap now".

  • Michael Lane

    Hi Lei Jhang,

    For .NET Framework Beta 2, you need to download the Beta Profiler from this link:
    http://download.microsoft.com/download/1/2/e/12ed42f8-1c9c-4f97-a969-581a61cc588c/clrprofilerb2.exe

    Regards,
    Vikram

  • Projyal

    Hi,

    It appears to me that the CLR Profiler is slow. Whatever you described to me and I saw a blank window for 4 minutes and then it came back and showed the heap. Make sure you give it sometime.

    Regards,
    Vikram

  • willmacs

    Yes, this is what I am currently using.

    Thanks.

  • Wraith Systems

    When the window stays blank, can you scroll the window or click the menu I can scroll it and click the menu after the progress bar (showing the log file is being loaded) disappear, so I think its work has finished.

    In general, the CLR Profiler will show the result in several seconds on my machine.


  • How can I know the reference count of an object?