title: Xamarin monodroid - how to track .net memory leaks
body: <p>I have lots of memory allocated in my mono droid program which doesn't appear to directly belong to either the Dalvik or the Mono heaps. Also, I can't figure out how to track .NET memory leaks.</p>
  
  <p>When I call</p>
  
  <p>adb shell dumpsys meminfo MyProgram.Droid</p>
  
  <p>This is the output:</p>
  
  <pre><code>** MEMINFO in pid 1364 [MyProgram.Droid] **
                       Shared  Private     Heap     Heap     Heap
                 Pss    Dirty    Dirty     Size    Alloc     Free
              ------   ------   ------   ------   ------   ------
     Native       36       24       36    38080    37775      124
     Dalvik     6934    15164     6572    16839    15384     1455
     Cursor        0        0        0
     Ashmem        0        0        0
  Other dev        4       36        0
   .so mmap    12029     2416     9068
  .jar mmap        0        0        0
  .apk mmap    16920        0        0
  .ttf mmap        3        0        0
  .dex mmap     2299      296        8
  Other mmap       64       24       36
    Unknown    28920     8216    28728
      TOTAL    67209    26176    44448    54919    53159     1579
  </code></pre>
  
  <p>I assume that the "Unknown" section is the mono framework, including the .NET heap. However when I call</p>
  
  <p>GC.GetTotalMemory(true)</p>
  
  <p>It tells me that only 5Mb of memory is allocated. That leaves 23Mb which I cannot trace (and there's 38Mb of allocated native heap)</p>
  
  <p>Additionally, I don't see that Xamarin have any tools for tracking .NET memory leaks. I've added garbage collection logging with</p>
  
  <pre><code>adb shell setprop debug.mono.log gc,gref
  </code></pre>
  
  <p>But this is incredibly verbose and hard to read and doesn't even include allocation sizes.</p>
  
  <p>At this point I don't know what to do to track down the resulting leaks. Since the allocations appear to be on the native heap, do I need to use the NDK in order to track down what's going on? Are there any tools I can use on the C# side to track the .NET leaks?</p>
  
  <p>Thanks!</p>