# Memory Leak

When the code is >10,000 lines of code (LoC), or >100,000 LoC, and we see memory of our research growing either slowly, or aggressively, it would eventually crash based on the compute profile used. The following steps can be used via windbg, gdb, and dotnet profiler for an incisive look to find the root cause without reading the entire codebase. We can think of this as a surgical intervention for super-quick turnaround and resolution, rather than, a way to defer cleaning-up bad codes after-the-fact.

# Method

Batch file for hammering service hammer.bat to amplify the leak for easier analysis, before either

  1. force-crashing execution into a dump (by waiting for crash, or manually crashing), or
  2. manually taking a running snapshot memory dump

Now, the below snippets of code can help analyse memory blocks and identify code-segments taking most space. As a rule of thumb, if it is indeed a memory-leak, taking snapshots at different time-intervals would result in marked-increase in specific memory symbols, while the rest of symbols stay similar in size. One needs to only identify which symbol(s) continually increase as time progresses, and then investigate the code manually for those objects created to either force garbage-collect, or rather re-write that specific code-object with better logic-flow to avoid dangling memory links.

# Hammering

:ping
REM t represents parallel threads, n represents unique-job-counts, w represents wait, z represents running code-segment
python concurrent_request.py -t 4 -n 10 -w 10 -z Local
goto ping

# Lookup to identify leak

Windbg
Cdb

<Load file of the crash dump in <dir1>>

Loads symbols
!analyze -v

Open log file
.logopen <filename>
.logclose

Dump heap group by type
!dumpheap -stat 

Dump heap group by type, rooted
!dumpheap -stat -live

Dump heap by type
!dumpheap -mt <methodtable address>

!dumpobject <address>

!dumparray <address>

!gcroot <address>

Dump all strings
.foreach /pS 7 /ps 4 ( methodtable {!Name2EE mscorlib.dll System.String}){ .foreach ( str {!dumpheap -short -live -mt ${methodtable}}) {.printf "SV (%p): %mu\n",${str},${str}+0xc}}

Dump memory range
b <addr> <addr>+42f18

# Graphs

Actual stress and memory leak investigation - x-axis in <memory-units>, y-axis in <time-units>
Actual stress and memory leak investigation - x-axis in <memory-units>, y-axis in <time-units>