Finding a memory leak in a .NET dump with an AI agent

 
 
  • Gérald Barré

This post is part of the series 'Debugging .NET with an AI agent'. Be sure to check out the rest of the blog posts of the series!

A leak investigation has three questions, in this order: what is on the heap, who keeps it alive, and is it actually growing. Each has its own command, and the third one is the one that gets skipped, by humans and models alike, because a single dump always looks like enough evidence.

The sample contains the application, the collection script, and the skill. Every command output below comes from it.

#The application that grows

A cache that never evicts anything, behind a static field:

C#
static class ResponseCache
{
    // A static field is a GC root: nothing in this dictionary can ever be collected
    private static readonly ConcurrentDictionary<string, CachedResponse> Entries = new();

    public static void Add(string key, CachedResponse response) => Entries[key] = response;
}

// called for every request
ResponseCache.Add($"key-{id++}", new CachedResponse { Payload = new byte[1024] });

The sample also has a churn mode that allocates far more and keeps nothing, which is used at the end of this post to show what a leak is not.

#Collecting the data

Three things, and the second one is what most people forget:

Shell
dotnet tool install --global dotnet-dump
dotnet tool install --global dotnet-gcdump
dotnet tool install --global dotnet-counters

# 1. the object graph, to find the roots
dotnet-dump collect --process-id 1234 --type Heap --output ./leak.dmp

# 2. two heap snapshots, 20 seconds or 20 minutes apart, to prove growth
dotnet-gcdump collect --process-id 1234 --output ./heap-t1.gcdump
sleep 20
dotnet-gcdump collect --process-id 1234 --output ./heap-t2.gcdump

# 3. a timeline, to tell a leak from a high allocation rate
dotnet-counters collect --process-id 1234 --counters System.Runtime --refresh-interval 2 \
    --duration 00:00:00:10 --format json --output ./counters.json

--type Heap is enough here: dumpheap and gcroot need the object graph, not the module images. A gcdump is much smaller than a dump, a few MB against a few hundred, and dotnet-gcdump collect triggers a blocking gen 2 collection, so on a large heap it pauses the process for a while. That is acceptable on a service that is already dying of memory, less so on a healthy one.

#What the skill brings to a leak

Same setup as the first post of this series, same SKILL.md. Three of its lines matter here, and each one exists because of a mistake models reliably make on a leak:

| Symptom      | Commands                                                    |
| ------------ | ----------------------------------------------------------- |
| Memory grows | dumpheap -stat (biggest types last), then gcroot <address>   |

- dumpheap -stat is sorted by total size ascending, so the interesting lines are the last ones
- A single dump cannot prove a memory leak, it only shows what is on the heap at one instant. To claim a
  leak, ask for a second snapshot taken later and compare

The last one is the important one, and it is the rule most likely to be missing from a skill you write yourself: a dump always has a biggest type, and a biggest type is not a leak.

Shell
dotnet-dump analyze ./leak.dmp -c "dumpheap -stat" -c "exit"

#The investigation

The prompt:

The memory of ./LeakDemo grows continuously. artifacts/ has a dump, two gcdumps and a counters timeline.
Find what leaks and who keeps it alive.

##What is on the heap

Shell
dotnet-dump analyze artifacts/leak.dmp -c "dumpheap -stat" -c "exit"
ffff58e77a70       1   2,595,616 System.Collections.Concurrent.ConcurrentDictionary<System.String, CachedResponse>+VolatileNode[]
ffff58ca6080 180,000   4,320,000 CachedResponse
aaaadd2dc3c0 113,287   7,317,664 Free
ffff58bddd58 181,814   7,466,930 System.String
ffff58e781c0 180,000   8,640,000 System.Collections.Concurrent.ConcurrentDictionary<System.String, CachedResponse>+Node
ffff58ca6138 180,011 188,668,364 System.Byte[]
Total 841,191 objects, 219,440,782 bytes

The output is sorted by total size ascending, so this is the tail of a list of thousands of types. 188 MB of byte[] and 180,000 CachedResponse, Node and String instances in matching counts: whatever holds one holds all three.

Free is not a type, it is the space between live objects. 7 MB of it here is normal; hundreds of MB would be a fragmentation story rather than a leak.

##Who keeps it alive

byte[] and String are always at the top of a heap. They are never the answer, only the payload. The answer is the type that owns them, so take one instance of the application type and ask for its roots:

Shell
dotnet-dump analyze artifacts/leak.dmp -c "dumpheap -mt ffff58ca6080" -c "exit"
         Address               MT           Size
    fff776000498     ffff58ca6080             24
Shell
dotnet-dump analyze artifacts/leak.dmp -c "gcroot fff776000498" -c "exit"
HandleTable:
    0000ffff97b213e8 (strong handle)
          -> fff774000028     System.Object[]
          -> fff77640ade0     System.Collections.Concurrent.ConcurrentDictionary<System.String, CachedResponse> (static variable: System.AppDomain.s_domain)
          -> fff77ec87a98     System.Collections.Concurrent.ConcurrentDictionary<System.String, CachedResponse>+Tables
          -> fff776e2e288     System.Collections.Concurrent.ConcurrentDictionary<System.String, CachedResponse>+VolatileNode[]
          -> fff77f5496b0     System.Collections.Concurrent.ConcurrentDictionary<System.String, CachedResponse>+Node
          -> fff77ed67de0     System.Collections.Concurrent.ConcurrentDictionary<System.String, CachedResponse>+Node
          -> fff776000498     CachedResponse

Found 2 unique roots.

gcroot prints the reference chain from a GC root down to the object. It starts at a strong handle, goes through a static ConcurrentDictionary<string, CachedResponse>, and ends on the object. A static field holding a dictionary that only grows: that is the diagnosis, and it took three commands.

One detail to know before trusting the label: the (static variable: System.AppDomain.s_domain) annotation is how SOS reports a static, not the name of the field in your code. The type arguments are what identify the culprit here, ConcurrentDictionary<String, CachedResponse>, not the annotation. The second root, not shown, is a temporary reference to the same dictionary on a thread stack.

This chain is also where an agent earns its keep. On a real application it is ten links long and passes through types nobody remembers registering: a middleware, a memory cache entry, an event handler, a captured closure. Following it by hand is tedious; following it is exactly what a machine is good at.

##Is it actually growing

Everything above is true of a healthy cache too. What makes it a leak is the second snapshot:

Shell
dotnet-gcdump report artifacts/heap-t1.gcdump > heap-t1.txt
dotnet-gcdump report artifacts/heap-t2.gcdump > heap-t2.txt
diff heap-t1.txt heap-t2.txt
# heap-t1.txt
     35,518,517  GC Heap bytes
        122,793  GC Heap objects
          1,048    30,000  System.Byte[] (Bytes > 1K)  [System.Private.CoreLib.dll]

# heap-t2.txt, 20 seconds later
    153,798,373  GC Heap bytes
        525,640  GC Heap objects
          1,048   130,000  System.Byte[] (Bytes > 1K)  [System.Private.CoreLib.dll]

35 MB to 153 MB in 20 seconds, and 30,000 to 130,000 buffers of 1,048 bytes. Two text files, a diff, and the growth is proven rather than assumed.

Note the first column of a dotnet-gcdump report: it is the size of one object, not the total. 1,048 bytes each, 30,000 then 130,000 of them. Sorting by that column produces a confident and wrong ranking, and dumpheap -stat, which does report totals, is the command to use when you want the biggest types.

#Allocation rate is not a leak

The counters timeline is what separates "this application allocates a lot" from "this application retains everything". Running the sample's two modes for ten seconds each:

Counterchurnthe leak
dotnet.gc.heap.total_allocated (per 2 s)~80 GB11.7 MB
dotnet.gc.last_collection.heap.size (gen2)0 to 146 KB151 MB → 197 MB
dotnet.process.memory.working_set41 MB, flat218 MB → 260 MB

The mode allocating several thousand times more memory is the healthy one: everything it allocates dies immediately, the gen 2 heap stays at a few hundred kilobytes, and the working set does not move. The leak allocates almost nothing by comparison and never gives anything back.

A high allocation rate is a CPU and GC pause problem, diagnosed with a trace. Retained size is what a dump measures, and it is the only thing that indicates a leak. Both belong in the skill, because "the application allocates a lot" is a conclusion a model reaches easily from a single dumpheap -stat.

#The fix

Bound the cache. Microsoft.Extensions.Caching.Memory with a size limit and an expiration, or a plain LRU, or simply removing the entry when the request ends. The general rule the dump points at: anything reachable from a static field lives for the lifetime of the process, so a static collection needs an eviction policy from the first day.

#What the agent gets wrong, and how to fix it

Every mistake below has a fix, and it is always one of three things: a line in the skill, a better prompt, or one command you run yourself.

It concludes from one dump. A dump always has a biggest type, and a model reports it as the leak.

Fix: rule 4 in the skill, which makes the agent ask for a second snapshot, and collecting the two gcdumps before asking. The prompt used in this post mentions them, so the agent uses them instead of asking.

It blames System.Byte[] or System.String. They are at the bottom of every dumpheap -stat, and "188 MB of byte arrays" sounds like an answer.

Fix: a skill line that names them as payload and says what to do next:

System.Byte[], System.String and Free at the end of dumpheap -stat are the payload, never the answer.
Run gcroot on an instance of an application type with a similar count instead

It reads the gcdump report as totals. Sorted by the first column, the report ranks types by the size of one instance.

Fix: the skill says so, in the list of other files:

*.gcdump: [...] The first column is the size of one object, not the total: use the count, or
dumpheap -stat on a dump, to rank types

It stops at the first root. gcroot prints "Found 2 unique roots" here, and a model may pick the one on the thread stack, which is a temporary.

Fix:

When gcroot finds several roots, say which one you kept and why. A root on a thread stack is usually a
temporary

The point of say why is that you can disagree with the reason, which you cannot do with a choice that was never stated.

The next post is the same investigation on an application that has no cache at all, and leaks anyway: the objects are held by an event handler and by a timer.

Do you have a question or a suggestion about this post? Contact me!

Follow me:
Enjoy this blog?