How to take heap snapshot of Xamarin.Android's Mono VM?
Asked Answered
M

2

6

Background: I am trying to track down a memory leak in a Xamarin.Android app. Using DDMS and Eclipse Memory Profiler, I am able to see which objects are alive. When trying to track what is holding them alive (GC Root), I only see "Native stack" (of course).

How can I take a heap snapshot of the MONO VM? So I can later use it with i.e. heapshot tool?

Or are there ANY OTHER TECHNIQUES I can use to find what is holding an object alive in Xamarin.Android's .NET part? Is it possible to do something from within the program?

Mcnulty answered 1/10, 2013 at 20:25 Comment(0)
S
13

How can I take a heap snapshot of the MONO VM? So I can later use it with i.e. heapshot tool?

It is now possible to get heap snapshots of the Mono VM (tested with Xamarin.Android 4.8.2 beta; may apply to prior releases, your mileage may vary). It's a four step process:

  1. Enable heapshot logging:

    adb shell setprop debug.mono.profile log:heapshot
    
  2. Start your app. (If your app was already running before (1), kill and restart it.)

    Use your app.

  3. Grab the profile data for your app:

    adb pull /data/data/@PACKAGE_NAME@/files/.__override__/profile.mlpd
    

    @PACKAGE_NAME@ is the package name of your application, e.g. if your package is FooBar.FooBar-Signed.apk, then @PACKAGE_NAME@ will be FooBar.FooBar.

  4. Analyze the data:

    mprof-report profile.mlpd
    

    mprof-report is included with Mono.

Note: profile.mlpd is only updated when a GC occurs, so you may want to call GC.Collect() at some "well known" point to ensure that profile.mlpd is regularly updated .

Slowworm answered 1/10, 2013 at 21:13 Comment(2)
Two options to mprof-report that may be useful: --traces lists the types that are holding references to objects on the heap and a reference count for each. --verbose yields a more complete list of references, mprof-report abbreviates the list by default.Lapsus
Is there any documentation on mlpd file format? Or a way to get computer readable data out of log file?Lorrin
S
4

I have been having troubles with Xamarin Android memory profiling, and have used a few tricks:

  1. On the Dalvik side I have used Android Monitor to dump a heap snapshot and then opening it with JProfiler or Eclipse MAT. This standard Android.

  2. A large portion of my code is shared (70-80%) and to verify this I have built a simple WinForms application to drive the shared API. This way I can use .Net Memory Profiler (or ANTS if you would prefer) as well as dotTrace for performance. I could easily pick quite a few issues this way.

  3. By using the solution explained by @jnop above I could open the profile.mldp in Mono's HeapShot tool and get a visual tool instead of the mprof-report textual output.

By the way used should vote for better profilers: http://xamarin.uservoice.com/forums/144858-xamarin-suggestions/suggestions/3229534-add-memory-and-performance-profiler

Scholem answered 11/2, 2014 at 14:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.