Collect Heap Snapshot vs Gen. 2 Garbage Collection

Use this forum for questions on how to use .NET Memory Profiler and how to analyse memory usage.
Post Reply
Dave
Posts: 6
Joined: Fri Jan 27, 2006 3:29 pm

Collect Heap Snapshot vs Gen. 2 Garbage Collection

Post by Dave » Fri Jan 27, 2006 3:42 pm

Before we started using the Memory Profiler, we had a key sequence that would trigger a garbage collection and wait for pending finalizers. However, when tracking memory usage on the Real-time tab, the key sequence triggers a Gen. 2 garbage collection and frees most of the memory, but some is not freed until Collect Heap Snapshot. What does Collect Heap Snapshot do that makes it more efficient than a normal garbage collection?

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

Post by Andreas Suurkuusk » Fri Jan 27, 2006 8:21 pm

The profiler doesn't perform anything special before collecting a heap snapshot, but it does perform more than one gen #2 garbage collect. When a heap snapshot is collected, actions equivalent to the following are performed:

Code: Select all

// Perform a full garbage collect
GC.Collect();  

// Perform another full garbage collect
GC.Collect();  

// Allow finalizers to run (we cannot call GC.WaitForPendingFinalizers from the profiler)
Thread.Sleep(100); 

// Perform a final full garbage collect and collect snapshot data
GC.Collect();   

I'm not sure that we actually need to call GC.Collect() twice before waiting for finalizers, but we have had indications that heap data is more thourougly collected when we do.
Best regards,

Andreas Suurkuusk
SciTech Software AB

Dave
Posts: 6
Joined: Fri Jan 27, 2006 3:29 pm

Post by Dave » Fri Jan 27, 2006 9:32 pm

Thanks for your response.

I've modified our code such that the key sequence does those same actions (except I left the GC.WaitForPendingFinalizers instead of the Thread.Sleep). However, the key sequence still does not return all of the memory - unless a task switch occurs in between. I trigger several garbage collections and nothing happens. I switch to another application and switch back, and the next garbage collection frees up the memory.

I realize that this has nothing to do with the Memory Profiler, but I'm hoping you might be able to shed some light on what happens to the memory during a task switch that allows it to be collected.

Thanks in advance!

Dave

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

Post by Andreas Suurkuusk » Mon Jan 30, 2006 10:18 am

When you are tracking the memory usage using the Real-time view, are you looking at the Total bytes or Live bytes count?

I don't believe that the profiler is responsible for this behaviour, but if it's possible for you, it would be good if you could provide us with a program that reproduces this behaviour.

You can contact us at support@scitech.se, if you have a program that you can provide us.
Best regards,

Andreas Suurkuusk
SciTech Software AB

Post Reply

Who is online

Users browsing this forum: No registered users and 24 guests