Taking a snapshot alters the state of my program

Use this forum for questions on how to use .NET Memory Profiler and how to analyse memory usage.
Post Reply
Peregrinati
Posts: 1
Joined: Wed Jan 30, 2008 11:17 pm

Taking a snapshot alters the state of my program

Post by Peregrinati » Wed Jan 30, 2008 11:23 pm

I have almost 6000 odd System.EventHandler's showing up in the realtime view of my C# application. When I take a snapshot (and *only* when I take a snapshot) it drops to around 300. Is this normal? It seems strange to me that taking a "picture" of something alters it's state. Quantum mechanics this is not :P

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

Post by Andreas Suurkuusk » Thu Jan 31, 2008 9:42 pm

I assume that the System.EventHandler instances you see in the real-time view are "Live instances"?

When a heap snapshot is collected the profiler should only present instances that are actually in use. To do this, the profiler will force the runtime to perform a full garbage collection and run the finalizers. Thus the memory usage will be affected when a heap snapshot is collected, since unused instances have been cleaned up. In your case, it is possible that the event handlers are referenced by instances that are waiting for finalization. When a snapshot is collected, the finalizers run, and the event handlers get released.

If you want to avoid side effects like this, you can enable heap utilization tracking and collect a generation #0 snapshot ("Collect Gen #0 Shapshot"). Additionally, when collecting a snapshot from an attached process, no GC is performed whatsoever. This will create a "true" snapshot, which has not altered the state of the "picture" at all (but less information will be included in the snapshot).
Best regards,

Andreas Suurkuusk
SciTech Software AB

MikeB
Posts: 7
Joined: Wed Dec 05, 2007 6:43 pm

Post by MikeB » Mon Jun 30, 2008 8:16 pm

Is there any way to force that garbage collection before taking a snapshot of an attached process?

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

Post by Andreas Suurkuusk » Tue Jul 01, 2008 10:35 am

In the current version of the profiler, it it not possible to perform an "invasive" attach and therefore a garbage collection cannot be performed.

When snapshots are collected from an attached process, the profiler only analyses the memory of the process. The process code execution is not affected in any way. But, in order to perform a garbage collection, the profiler would need to execute code in the profiled process (i.e. invoke the garbage collector).
Best regards,

Andreas Suurkuusk
SciTech Software AB

MikeB
Posts: 7
Joined: Wed Dec 05, 2007 6:43 pm

Post by MikeB » Tue Jul 01, 2008 2:39 pm

What is the best practice then? If I am comparing two snapshots, I'm not necessarily getting a true representation of what hasn't been garbage collection, right, since I have no way of asking the system to perform a garbage collection? This is particularly evident when I see a negative number in the delta column after permorming an operation that I know shouldn't be removing objects. Is there a better way to "aim" snapshots directly after garbage collections?

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

Post by Andreas Suurkuusk » Wed Jul 02, 2008 8:32 pm

The best practice would be to start the profiled process from the profiler if possible (e.g. using File->Profile application). This provides more control over the profiled process and much more detailed information is presented. If you must attach to the process (e.g. if you're doing production code profiling), then it's currently not possible to "aim" snapshots directly after garbage collections.

However, when you collect a snapshot from an attached process, only reachable instances are included in the "Live instances" count. The lack of a GC should not significantly affect the instance count, unless finalizable instances are involved.
Best regards,

Andreas Suurkuusk
SciTech Software AB

Post Reply

Who is online

Users browsing this forum: No registered users and 18 guests