Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

โ€œInvest in yourself โ€” your confidence is always worth it.โ€

Explore Cosmetic Hospitals

Start your journey today โ€” compare options in one place.

DOTNET: Understanding Memory leaks and Debugging using dotMemory

Iโ€™ll give you:

  1. One self-contained Program.cs you can drop into a new project
  2. How to run it from command line
  3. Step-by-step: how to use dotMemory to detect the leak
  4. How to read dotMemory results & locate the leak
  5. How to fix it (clean version of the code)

1๏ธโƒฃ Create the demo app

dotnet new web -n MemoryLeakDemo
cd MemoryLeakDemo
Code language: JavaScript (javascript)

Now replace the entire Program.cs with this:

This is self-sufficient: one file, no extra classes, no extra projects.


2๏ธโƒฃ Run the app (command line)

From the MemoryLeakDemo folder:

dotnet run

It will listen on something like:

  • http://localhost:5000
  • https://localhost:5001

(Use the HTTP one for simplicity: http://localhost:5000)


3๏ธโƒฃ Generate the leak

In another terminal (or browser / Postman):

A. Start leaking memory

Run a loop (PowerShell):

1..50 | ForEach-Object {
    curl "http://localhost:5000/leak"
}
Code language: JavaScript (javascript)

Or Git Bash:

for i in {1..50}; do curl -s "http://localhost:5000/leak" > /dev/null; done
Code language: JavaScript (javascript)

Then check stats:

curl "http://localhost:5000/stats"
Code language: JavaScript (javascript)

You should see Total stored MB growing (e.g. 250 MB).

B. Compare with non-leaking allocations

1..50 | ForEach-Object {
    curl "http://localhost:5000/noleak"
}
curl "http://localhost:5000/gc"
curl "http://localhost:5000/stats"
Code language: JavaScript (javascript)
  • /noleak allocates memory but does not store it โ†’ GC can clean it.
  • /leak allocates memory and keeps it in a static list โ†’ cannot be GCโ€™d.

4๏ธโƒฃ Profile with dotMemory โ€“ step by step

Assuming you have dotMemory GUI (Rider/Standalone):

Step 1: Start the app

Make sure dotnet run for MemoryLeakDemo is running.

Step 2: Open dotMemory

  • Start dotMemory
  • Choose โ€œProfile Running Processโ€ (or similar wording)
  • Select the dotnet.exe / MemoryLeakDemo.dll process
  • Click Run / Attach

Step 3: Take a baseline snapshot

In dotMemory:

  • Click โ€œGet Snapshotโ€ (or โ€œGet Snapshot #1โ€).
  • Name it โ€œBaseline (before leak)โ€.

Step 4: Generate the leak

Back in terminal:

1..100 | ForEach-Object {
    curl "http://localhost:5000/leak"
}
curl "http://localhost:5000/stats"
Code language: JavaScript (javascript)

You should see big total MB stored.

Step 5: Take snapshot after leak

In dotMemory:

  • Click โ€œGet Snapshotโ€ again.
  • Name it โ€œAfter leak (static list)โ€.

Step 6: Optionally, force GC and snapshot

Back in terminal:

curl "http://localhost:5000/gc"
curl "http://localhost:5000/stats"
Code language: JavaScript (javascript)

Then in dotMemory:

  • Take Snapshot #3: โ€œAfter GCโ€.

5๏ธโƒฃ Understanding dotMemory results (how to spot the leak)

Now the fun part: reading dotMemory.

A. Compare snapshots (Baseline vs After leak)

  1. In dotMemory, select โ€œBaselineโ€ and โ€œAfter leakโ€ snapshots.
  2. Click โ€œCompare Snapshotsโ€.

Look at:

  • Heap size increased a lot
  • New objects count is huge
  • Top types by size will likely show System.Byte[] (byte arrays)

B. Drill into byte[] and find retention

Click on System.Byte[] and open:

  • โ€œRetention Graphโ€
  • or โ€œShortest Paths to GC Rootsโ€

You should see a path like:

Static field โ†’ LeakStore.Buffers โ†’ List<byte[]> โ†’ byte[]
Code language: CSS (css)

This tells you:

  • The byte[] objects are alive because they are referenced by a static list.
  • That static list is LeakStore.Buffers.

This is your memory leak.

C. Check what happens after GC

Compare โ€œAfter leakโ€ vs โ€œAfter GCโ€ snapshots:

  • If byte[] objects are still around in large numbers/size โ†’ GC cannot free them.
  • Why? Because theyโ€™re still referenced by LeakStore.Buffers.

That is exactly how dotMemory exposes real leaks:

Objects survive GCs and are retained by long-lived references (statics, singletons, caches, events, etc.).


6๏ธโƒฃ Common real-world patterns similar to this leak

  • Static lists/dictionaries that grow but never shrink
  • Caches without eviction policies
  • Event handlers where you forget to -= unsubscribe
  • Long-lived singletons holding request-scoped data
  • Task or Timer callbacks closing over large objects
  • In-memory queues that never get drained

dotMemory will show all of these as:

  • Big retained size
  • GC roots pointing to static fields, singletons, or event tables

7๏ธโƒฃ How to fix this leak (clean code version)

Hereโ€™s a fixed version of the leak part:

Option A: Limit the cache

static class LeakStore
{
    private const int MaxBuffers = 10;
    private static readonly Queue<byte[]> Buffers = new();

    public static void AddBuffer(byte[] buffer)
    {
        Buffers.Enqueue(buffer);
        while (Buffers.Count > MaxBuffers)
        {
            Buffers.Dequeue(); // old buffers become eligible for GC
        }
    }

    public static int Count => Buffers.Count;
}
Code language: PHP (php)

Then in /leak:

var buffer = new byte[bytes];
// ...
LeakStore.AddBuffer(buffer);
Code language: JavaScript (javascript)

Now dotMemory will show:

  • Heap size not growing unbounded
  • Byte arrays being collected over time

Option B: Donโ€™t use static leaks at all

  • Use scoped dependencies
  • Avoid unbounded global collections
  • Use proper cache with TTL / LRU (e.g., MemoryCache)

8๏ธโƒฃ In-Code Metrics (like we did for TLS)

For this example, /stats already gives:

  • Number of stored buffers
  • Total MB stored
  • Number of leak calls

You can show this in the terminal while dotMemory shows heap graph growth โ†’ this connects code behavior + profiler view.


Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals
Iโ€™m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at <a href="https://www.cotocus.com/">Cotocus</a>. I share tech blog at <a href="https://www.devopsschool.com/">DevOps School</a>, travel stories at <a href="https://www.holidaylandmark.com/">Holiday Landmark</a>, stock market tips at <a href="https://www.stocksmantra.in/">Stocks Mantra</a>, health and fitness guidance at <a href="https://www.mymedicplus.com/">My Medic Plus</a>, product reviews at <a href="https://www.truereviewnow.com/">TrueReviewNow</a> , and SEO strategies at <a href="https://www.wizbrand.com/">Wizbrand.</a> Do you want to learn <a href="https://www.quantumuting.com/">Quantum Computing</a>? <strong>Please find my social handles as below;</strong> <a href="https://www.rajeshkumar.xyz/">Rajesh Kumar Personal Website</a> <a href="https://www.youtube.com/TheDevOpsSchool">Rajesh Kumar at YOUTUBE</a> <a href="https://www.instagram.com/rajeshkumarin">Rajesh Kumar at INSTAGRAM</a> <a href="https://x.com/RajeshKumarIn">Rajesh Kumar at X</a> <a href="https://www.facebook.com/RajeshKumarLog">Rajesh Kumar at FACEBOOK</a> <a href="https://www.linkedin.com/in/rajeshkumarin/">Rajesh Kumar at LINKEDIN</a> <a href="https://www.wizbrand.com/rajeshkumar">Rajesh Kumar at WIZBRAND</a> <a href="https://www.rajeshkumar.xyz/dailylogs">Rajesh Kumar DailyLogs</a>

Related Posts

Top 10 AI Audit Sampling Optimization Tools: Features, Pros, Cons & Comparison

Introduction AI Audit Sampling Optimization Tools are platforms that use artificial intelligence, statistical modeling, and data analytics to improve how audit samples are selected, tested, and validated….

Read More

Top 10 AI GRC Evidence Collection Tools: Features, Pros, Cons & Comparison

Introduction AI GRC Evidence Collection Tools are platforms that help organizations automatically gather, organize, and validate compliance evidence across systems, applications, and workflows using AI-driven automation. In…

Read More

Top 10 AI Third-Party Risk Analytics Tools: Features, Pros, Cons & Comparison

Introduction AI Third-Party Risk Analytics tools are platforms that help organizations assess, monitor, and manage risks originating from external vendors, suppliers, partners, and service providers. These systems…

Read More

Top 10 AI Insider Trading Risk Detection Tools: Features, Pros, Cons & Comparison

Introduction AI Insider Trading Risk Detection tools use machine learning, natural language processing (NLP), behavioral analytics, and network graph modeling to identify suspicious trading behavior that may…

Read More

Top 10 AI AML Case Triage Assistants: Features, Pros, Cons & Comparison

Introduction AI AML (Anti-Money Laundering) Case Triage Assistants are intelligent systems designed to help financial institutions automatically prioritize, classify, investigate, and escalate suspicious financial activities. These tools…

Read More

Top 10 AI KYC Identity Verification with ML Tools: Features, Pros, Cons & Comparison

Introduction AI KYC (Know Your Customer) Identity Verification with Machine Learning refers to intelligent systems that verify customer identities using AI-powered document analysis, facial recognition, liveness detection,…

Read More
Subscribe
Notify of
guest
2 Comments
Newest
Oldest Most Voted
Jason Mitchell
Jason Mitchell
6 months ago

Excellent and very practical walkthrough โ€” this article demystifies how memory leaks can still happen in managed environments like .NET, and why simply relying on garbage collection isnโ€™t enough. I like how it explains that leaks occur when objects remain referenced (for example through static collections, event subscriptions, caches or forgotten disposals), preventing garbageโ€‘collector from reclaiming memory. The demonstration of using dotMemory to capture memory snapshots, compare memory traffic over time, and spot objects that linger unnecessarily makes the debugging process concrete and approachable. For any .NET developer concerned about application stability or memory bloat, this guide is a strong call to adopt memoryโ€‘profiling and leakโ€‘detection as part of regular development cycles. ๐Ÿ‘

Skylar Bennett
Skylar Bennett
6 months ago

This article offers a clear and insightful explanation of memory leaks in .NET applications and how to effectively debug them using dotMemory. It highlights common causes of memory leaks, such as static references or event handlers that aren’t properly unsubscribed, which prevent the garbage collector from cleaning up memory. The practical walkthrough with dotMemory tools, including how to analyze memory snapshots, identify retained objects, and trace their references, provides valuable hands-on experience for developers. By demonstrating how to track down memory issues and optimize resource management, the post serves as an excellent resource for anyone looking to improve the performance and reliability of their .NET applications.

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