Step-by-Step: Optimizing Production Code with NP .NET Profiler

Written by

in

To troubleshoot memory leaks using SciTech’s ⁠.NET Memory Profiler (commonly known as NP), you must isolate surviving objects by comparing distinct memory states.

The primary workflow consists of capturing two distinct snapshots—one before an action and one after—and analyzing the “New Instances” to find their roots. Step 1: Establish a Baseline (Snapshot 1)

Launch .NET Memory Profiler and attach it to your running application or boot the app directly through the tool.

Navigate your application to a neutral, steady “Idle” state (e.g., the main dashboard or a quiet menu).

Click Collect Snapshot. The tool will automatically force a full Garbage Collection (GC) so you only capture objects actively held in memory. Step 2: Trigger the Suspected Leak Scenario

Perform the heavy operation or user interaction inside your app that you suspect causes the leak (e.g., opening a document, processing a massive batch job, or spinning up temporary threads).

Return your application back to the exact same Idle state from Step 1. Step 3: Capture the Second State (Snapshot 2) Click Collect Snapshot again.

The profiler will trigger another full GC, meaning any transient or properly disposed memory will clear out. Any objects remaining are surviving instances that could not be freed. Step 4: Compare Snapshots & Isolate the Leak

Select the Comparison View inside the profiler to diff Snapshot 1 against Snapshot 2.

Look immediately at the New instances column or filter by the highest Instance-count difference.

Identify the managed classes or custom objects showing an unexpected net positive growth. Step 5: Trace the Paths to the GC Root

Double-click the offending class type to look at its active list of live objects.

Select a leaked instance and review the Object Retention Graph (Path to GC Root).

This visual hierarchy tree maps exactly which long-lived object or reference chain is keeping your temporary object alive. 💡 Common Culprits to Look For YouTube·tutorialsEU – C# Finding MEMORY LEAKS in C# .NET Applications

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *