How can I know the reference count of an object?
object obj = ...
...
int count = GetReferenceCount(obj);
if (count > 0)
{
Debug.WriteLine(...);
}
object obj = ...
...
int count = GetReferenceCount(obj);
if (count > 0)
{
Debug.WriteLine(...);
}
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
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.
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
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
In general, the CLR Profiler will show the result in several seconds on my machine.