Turn Your Vehicle Into a Smart Earning Asset

While you’re not driving your car or bike, it can still be working for you. MOTOSHARE helps you earn passive income by connecting your vehicle with trusted renters in your city.

🚗 You set the rental price
🔐 Secure bookings with verified renters
📍 Track your vehicle with GPS integration
💰 Start earning within 48 hours

Join as a Partner Today

It’s simple, safe, and rewarding. Your vehicle. Your rules. Your earnings.

dotnet-gcdump — lightweight .NET memory profiling tool Advanced Tutorial



1. What is dotnet-gcdump?

dotnet-gcdump is a lightweight .NET memory profiling tool that collects GC (Garbage Collector) heap dumps with minimal performance overhead.

It gives you:

  • Managed heap snapshot
  • Objects allocation information
  • Object types, sizes, counts
  • Reference trees (roots, retainers)
  • Memory pressure analysis

It is part of the .NET diagnostics toolset along with:

  • dotnet-counters
  • dotnet-trace
  • dotnet-dump
  • dotnet-monitor

💡 Think of dotnet-gcdump as the “lightest way” to understand memory usage without pausing/stopping the app.


2. Why Use dotnet-gcdump?

✔ Extremely low overhead

You can capture memory dumps on live production apps with almost zero impact.

✔ Perfect for memory leak detection

It shows object types and counts to help identify:

  • Memory leaks
  • Excessive retained objects
  • High fragmentation
  • Objects not being freed

✔ Works cross-platform

Runs on Windows, Linux, and macOS.

✔ Great for comparing memory snapshots over time

Useful for before/after deployment analysis.


3. When to Use dotnet-gcdump?

Use it whenever you see:

🟡 Increasing memory usage in Production

  • Memory slowly goes up → leak
  • Too many pinned objects → fragmentation
  • Large object heap (LOH) too high

🟡 GC performance issues

  • High GC frequency
  • High fragmentation
  • Slowness or pauses

🟡 Investigate specific request memory spikes

Take a snapshot:

Before request → gcdump → After request

🟡 Compare two environment behaviors

  • Prod vs Stage
  • .NET version upgrade comparison
  • New features memory comparison

4. How to Install dotnet-gcdump

dotnet tool install --global dotnet-gcdump
Code language: PHP (php)

Verify:

dotnet-gcdump --version

5. How to Use dotnet-gcdump

A. Find your running .NET process

dotnet-gcdump ps

Example output:

  5120   MyApp.Api
  5566   Orders.Web
  7788   BackgroundWorker
Code language: CSS (css)

B. Collect a GC dump from a running process

dotnet-gcdump collect --process-id 5566

You will get:

Writing gcdump to MyApp_20251125_142410.gcdump
Complete
Code language: CSS (css)

C. Collect a dump from a process that will start now

dotnet-gcdump collect -- myapp.exe
Code language: CSS (css)

D. Specify output file

dotnet-gcdump collect --process-id 5566 --output /tmp/myapp-leak.gcdump

6. How to Analyze a GCDump

✔ Best tool: PerfView

(Microsoft official memory analysis tool)

Open PerfView →
File → Open → .gcdump file

You will see:

You can inspect:

✔ Heap Size

✔ Object Types

✔ Count & Size

✔ Roots

✔ Retention Graph

✔ Diff View (compare two dumps)


7. Important Metrics in a GC Dump

MetricMeaningWhy Important
Total Heap SizeTotal memory used by managed heapDetect memory bloat
Gen 0/1/2 SizeMemory per generationShows GC pressure
LOH (Large Object Heap)Objects > 85 KBHigh LOH = fragmentation risk
Pinned ObjectsObjects fixed in memoryAffects GC performance
Object CountHow many objects of each typeShows leaks (e.g., growing list)
Retainers / RootsWhat is holding objects aliveCritical for leak detection
Diff ViewCompare two snapshotsIdentifies what grew

8. Common Memory Issues Detected via GCDump

🔥 Memory Leaks

  • Singleton holding references
  • Event handlers not unsubscribed
  • Static lists growing

🔥 Large Object Heap Pressure

  • Large arrays
  • Images / byte[]
  • Serialization buffers

🔥 Excessive Allocation

  • Chatty LINQ
  • Unoptimized JSON serialization
  • Inefficient loops

🔥 Pinned Objects Issues

  • GPU buffers
  • Unsafe code
  • Interop

9. Advanced Usage

A. Collect after forcing GC (for cleaner dump)

dotnet-gcdump collect --process-id 5566 --gc-collect

B. Collect for a process name (not PID)

dotnet-gcdump collect --name Orders.Api
Code language: CSS (css)

10. Advantages of dotnet-gcdump

FeatureBenefit
LightweightSafe in production
Zero-impact on performanceUnlike full dump
Easy to automateCI, cron, scripts
Cross-platformWorks everywhere
Works with PerfViewPowerful analysis UI
Can compare snapshotsDetect leaks precisely

11. Limitations of dotnet-gcdump

LimitationNotes
Cannot debug code executionUse dotnet-trace
Only managed heapNo native memory
Limited call stacksNot full session trace
No CPU profilingUse dotnet-trace

12. Best Practices

⭐ Capture multiple dumps

Before, during, after load test.

⭐ Always analyze with PerfView

It is the official recommended tool.

⭐ Automate weekly GC dumps in production

Useful for long-running apps.

⭐ Combine with dotnet-counters

To monitor GC in real-time.



1. Basic Example — Capture a GC Dump from a Running Process

Step 1 — List running .NET processes

dotnet-gcdump ps

Example Output:

  5120   Orders.Api
  5566   LoginService
  7788   BackgroundWorker
Code language: CSS (css)

Step 2 — Capture a GC dump from PID 5120

dotnet-gcdump collect --process-id 5120

Output Example:

Writing gcdump to OrdersApi_20251125_141800.gcdump...
Complete
Code language: CSS (css)

👉 This creates a file like:

OrdersApi_20251125_141800.gcdump
Code language: CSS (css)

2. Example — Capture Dump of a Process by Name

If you don’t want to use PID:

dotnet-gcdump collect --name Orders.Api
Code language: CSS (css)

Output:

Found process 'Orders.Api' (PID 5120)
Writing gcdump...
Complete
Code language: JavaScript (javascript)

3. Example — Capture Dump and Save to Custom Path

dotnet-gcdump collect \
  --process-id 5566 \
  --output /logs/memory/Leak_5566_2PM.gcdump

4. Example — Collect After Forcing a Full GC

Useful when you want a clean snapshot:

dotnet-gcdump collect --process-id 5566 --gc-collect

Output:

Forcing full garbage collection...
Dumping managed heap...
Success

5. Example — Capture GCDump When Running a Command

Run your app and immediately collect:

dotnet-gcdump collect -- dotnet run --project MyApp.Api
Code language: CSS (css)

This starts MyApp.Api, collects dump, and exits.


6. Example — Compare Two GCDumps (using PerfView)

  1. Take two dumps: dotnet-gcdump collect --process-id 5120 --output baseline.gcdump dotnet-gcdump collect --process-id 5120 --output after_load.gcdump
  2. Open PerfViewDiff
  3. Select both dumps → Analyze differences.

PerfView will show:

  • Object growth
  • Memory leaks
  • Which objects increased or decreased

🚀 Real Production-like Examples

Example 7 — Detect Memory Leak in ASP.NET Core App

You see memory keeps increasing in production.
Take a gcdump:

dotnet-gcdump collect --process-id 44312 --output before_load.gcdump
Code language: CSS (css)

Run a heavy load test for 10 minutes…

Take second dump:

dotnet-gcdump collect --process-id 44312 --output after_load.gcdump
Code language: CSS (css)

Analyze diff:

  • List<SessionObject> grew by +120,000 objects
  • byte[] objects in LOH increased
  • Some static dictionary retained references

Result → Memory leak confirmed.


Example 8 — Identify Large Object Heap (LOH) Pressure

App is slow. Using gcdump:

dotnet-gcdump collect --process-id 5566

PerfView shows:

  • LOH: 600 MB
  • Large arrays: byte[] of sizes 1 MB each
  • Created during image resizing logic

Solution:
Switch to shared buffers, or pooling.


Example 9 — High Allocation Rate Investigation

dotnet-counters shows Allocation Rate 800 MB/sec.

Take gc-dump:

dotnet-gcdump collect --process-id 6632 --output high_alloc.gcdump
Code language: CSS (css)

PerfView:

  • Huge number of JObject and JToken
  • Caused by unoptimized JSON-parsing inside loop

🧪 Sample GCDump File Name Examples

Dump filenames generated automatically:

OrdersApi_20251125_091000.gcdump
PaymentService_20251125_152200.gcdump
BackgroundWorker_20251126_004500.gcdump
Code language: CSS (css)

📄 Sample dotnet-gcdump Output (Realistic)

Example console output when capturing:

PS> dotnet-gcdump collect --process-id 5566 --output api-memory-dump.gcdump

StartTime:       2025-11-25T14:15:03.123Z
Process Name:    Orders.Api
PID:             5566
GC Mode:         Server GC
Collecting...
Writing gcdump to 'api-memory-dump.gcdump'...
Complete
Dump Size:       85.32 MB
Code language: JavaScript (javascript)

🔍 Example PerfView Analysis (What You See)

Once opened in PerfView, you may see:

✔ Heap Summary

  • Total Heap: 1.2 GB
  • Gen 0: 120 MB
  • Gen 1: 80 MB
  • Gen 2: 350 MB
  • LOH: 650 MB

✔ Top Object Types by Size

TypeCountSize
byte[]2,450420 MB
JObject50,000200 MB
Dictionary<string, object>8,000130 MB
String450,00080 MB

✔ Retention Graph

You will see:

Static: SomeCache
 └── Dictionary<string, object>
       └── DataObject[]
             └── byte[]
Code language: CSS (css)

This tells you what is keeping objects alive → leak root.


🎯 Want me to generate a sample .gcdump file for training?

I can generate:

  • Fake GC dump
  • PerfView screenshots
  • Step-by-step lab manual
  • PPT (PowerPoint) 10–15 slides

Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x