roots for particular object

Use this forum for questions on how to use .NET Memory Profiler and how to analyse memory usage.
Post Reply
m_kramar
Posts: 1
Joined: Mon Mar 24, 2008 8:50 am

roots for particular object

Post by m_kramar » Mon Mar 24, 2008 9:05 am

Hello,

I have a collection class and a Remove function like this:

class MyCollection
{
private List<MyObject> m_List = new List<MyObject>();
.....

void Remove(MyObject obj)
{
m_List.Remove(obj);
}
}

MyCollection normally contains a lot of objects, let's say 1000. I delete several, let's say 2. I expect these 2 objects collected, but it seems they are rooted.

Question: Is there a way to get roots for these 2 particular objects?

It would be handy to have a code like this:

class MyCollection
{
private List<MyObject> m_List = new List<MyObject>();
.....

void Remove(MyObject obj)
{
m_List.Remove(obj);

#if DEBUG
int address = Profiler.GetAddress(obj);
RootInfo[] roots = Profiler.GetRoots(address);
Debug.Assert(roots.Count == 1); // make sure there is only one root for this object
#endif
}
}

Now let me explain why I'm not happy with existing profiler functionality. I take a snapshot; then when I expect garbage objects collected I take second screenshot. In the list of classes I find MyClass. I can see 1000 instances of that class. I can see allocation callstack for each of them. I can see roots for each of them. But 2 of them must have been collected. I mean there are 1000 objects in the list and I cannot find the 2 that must have been deleted. I cannot loop through all 1000 objects and examine their roots. Even if I do so, how do I know which ones are the 2 I'm looking for?

Thank you for assistance,
Mike

Andreas Suurkuusk
Posts: 1029
Joined: Wed Mar 02, 2005 7:53 pm

Post by Andreas Suurkuusk » Mon Mar 24, 2008 9:16 pm

If I understand your question correctly, you have removed 2 instances from the list, but after you collect a heap snapshot in the profiler, the two instances have not been collected.

If the instances are only supposed to be rooted by your list, then the removed instances probably have a different shortest root path. Have you tried to look at the "Shortest root paths" in the Type details. Hopefully the shortest root path will help you identify the instances that are no longer part of the list, but that are still alive.

Your suggestion on how to programmatically detect that a specific instance has not been GCed is good. The next version of .NET Memory Profiler will include improvements on the memory assertions and other ways to programmatically detect memory and resource problems. We will take your suggestion in consideration when working with this.
Best regards,

Andreas Suurkuusk
SciTech Software AB

Post Reply

Who is online

Users browsing this forum: No registered users and 20 guests