Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Xamarin.android memory management gotchas

2,294 views

Published on

Presented by Matthew Robbins at the September 2015 meetup of the Sydney Mobile .Net Developers Group

Published in: Mobile
  • Be the first to comment

Xamarin.android memory management gotchas

  1. 1. Xamarin.Android Memory Management Gotchas
  2. 2. Overview Xamarin.Android Memory Concepts ● Xamarin.Android Memory Spaces ● Framework vs User peers Activity Leaks ● Why they are bad ● Tools to track them down Demonstrations ● Obvious leak ● Not so obvious leak Summary and Questions
  3. 3. Introduction About Me ● Xamarin developer for 2.5 years ● Specialise in Xamarin.Android ● Currently work at TouchStar ○ Building Android Apps ● Building MonoDevelop.Addins ○ More on that at the end...
  4. 4. Xamarin.Android Memory Spaces Managed ● Mono / .NET space ● Boring :| Native ● Java objects that live only in ART/Dalvik ● Boring :| Peer ● Have a .NET and Java object ● Interesting!
  5. 5. Xamarin.Android Memory Spaces Reside only in Dalvik/ART space Reside only in Mono space Have object in Mono and Dalvik
  6. 6. Xamarin.Android Memory Spaces Peer Objects ● 2 halves: ○ Managed ○ Native ● 2 types ○ User peers ○ Framework peers
  7. 7. Xamarin.Android Memory Spaces Peer Objects ● Global reference in their Android Callable Wrapper (ACW). ○ This prevents either Java or Mono GC from reclaiming whilst in use in either VM. ○ Is severed when you dispose the managed peer, allowing both VMs to reclaim. ● Can inspect the ACW in the obj directory: ○ [Project-Path]/obj/[Configuration]/android/src/
  8. 8. Peer Objects User Peers ● .NET objects with Java object peer. ● Derive from Java.Lang.Object. ○ Therefore have a ACW generated ● Called user peers as they are defined by us, the user!
  9. 9. Peer Objects Managed Object
  10. 10. Peer Objects Native Peer
  11. 11. Peer Objects Framework Peers ● Native half points to a framework component. EG: ○ Activity ○ Service ○ Fragment ● Have potential to leak a lot of memory! ○ We will focus on Activity.
  12. 12. Peer Objects Activity Root View Fragment Adapter Bitmap GREF GREF GREF GREF Java Activity 2Mb image 1-N Items with views 1-N Subcomponents N+ Subviews Managed Peer
  13. 13. Activity Leaks Some Context... ● Activity derives from Context ○ Hence is commonly passed around into child components ● A context is: ○ Interface to global information about application environment. ○ Used to: ■ Start services/Activities ■ Get resources (strings, images, layouts etc) ■ Start activities
  14. 14. Activity Leaks Some Context... ● Activity derives from Context ○ Hence is commonly passed around into child components ● A Context is: ○ Interface to global information about application environment. ○ Used to: ■ Start services/Activities ■ Get resources (strings, images, layouts etc) ■ Get system services.
  15. 15. Detecting Activity Leaks Methods ● StrictMode ● Android Debug Bridge (ADB)
  16. 16. Detecting Activity Leaks StrictMode ● Developer tool for detecting accidental operations ○ File IOs on UI thread ○ Network operations on UI thread ○ Activity leaks! ● Commonly used to prevent ANRs ● Also very handy in preventing context leaks! http://developer.android.com/reference/android/os/StrictMode.html
  17. 17. Detecting Activity Leaks Configuring StrictMode var builder = new StrictMode.VmPolicy.Builder (); var policy = builder.DetectActivityLeaks ().PenaltyLog ().Build (); StrictMode.SetVmPolicy (policy); Put inside application entry point (IE: Activity.OnCreate())
  18. 18. Detecting Activity Leaks Strict Mode Output E/StrictMode(13257): android.os.StrictMode$InstanceCountViolation: class leakyactivity.MainActivity; instances=2; limit=1 E/StrictMode(13257): at android.os.StrictMode.setClassInstanceLimit(StrictMode.java:1)
  19. 19. Detecting Activity Leaks Android Debug Bridge ● Developer tool for “remoting” into Android devices ○ Kinda like using ssh ○ Lots of handy utilities! ■ pull, push, reboot ● Used in a terminal ● Also very handy in preventing context leaks! http://developer.android.com/reference/android/os/StrictMode.html
  20. 20. Detecting Activity Leaks Android Debug Bridge ● Useful command for watching memory growth: ○ adb shell dumpsys meminfo [package-name] ● Will: ○ Dump heap usage ○ Show View count ○ Show active Contexts ○ Show Activity count ○ Plus more!
  21. 21. Detecting Activity Leaks Android Debug Bridge
  22. 22. Detecting Activity Leaks Other tools ● Xamarin Profiler ○ https://xamarin.com/profiler ● Dalvik Debug Monitor Service (DDMS) ○ http://developer.android.com/tools/debugging/ddms.html ● Monitor ○ Found in [android-sdk-path]/tools/monitor.exe
  23. 23. Example One
  24. 24. Example One We learnt... ● StrictMode can highlight activity leaks. ● Dispose() objects with framework or ACW peers. ● GC.Collect() can often release un-disposed objects. ○ But don’t rely on it!
  25. 25. Example Two
  26. 26. Example Two What we learnt... ● Be wary of closures. ● Bind and unbind event handlers. ● Be wary of passing around Activity references for context API access. ○ If in doubt, use the global context reference ● Use ADB to watch effects of context leaks.
  27. 27. Do’s and Don’ts Do ● Dispose and null framework peers in ● Use the global context for resource access. ● Register-unregister event handlers Don’t ● Keep static references to activities. ● Use anonymous delegates (where a framework peer might get enclosed) ● Pass Activity references around if can be avoided; use global context instead.
  28. 28. Summary Key Concepts ● Managed VS Java VS Peer objects ● Framework VS ACW objects ● Framework peer leaks ○ Specifically Activity Examples ● Obvious Leak ● Not-so-obvious leak Useful Tools ● StrictMode ● ADB
  29. 29. Resources Xamarin Docs ● https://developer.xamarin.com/guides/android/advanced_topics/garbage_collection/ ● https://developer.xamarin. com/guides/android/advanced_topics/java_integration_overview/working_with_jni/ Android Docs ● http://developer.android.com/reference/android/os/StrictMode.html ● http://developer.android.com/tools/help/monitor.html ● https://developer.android.com/tools/debugging/debugging-memory.html
  30. 30. Lastly... Ideas for further talks? ● More memory discussion? ● Addin development? ● Other Xamarin.Android concepts? Building tooling for Xamarin Studio ● Android resource ○ Refactoring ○ Analysis ● Get in contact if you want to alpha-test
  31. 31. Contact Me! Links ● https://github.com/matthew-ch-robbins ● https://au.linkedin.com/pub/matthew- robbins/17/8a7/139 Email: ● matthew.ch.robbins@gmail.com

×