xamarin.android memory management gotchas
TRANSCRIPT
Xamarin.Android Memory Management Gotchas
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
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...
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!
Xamarin.Android Memory Spaces
Reside only in Dalvik/ART space
Reside only in Mono space
Have object in Mono and Dalvik
Xamarin.Android Memory Spaces
Peer Objects
● 2 halves:
○ Managed
○ Native
● 2 types
○ User peers
○ Framework peers
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/
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!
Peer Objects
Managed Object
Peer Objects
Native Peer
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.
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
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
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.
Detecting Activity Leaks
Methods
● StrictMode
● Android Debug Bridge (ADB)
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
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())
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)
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
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!
Detecting Activity Leaks
Android Debug Bridge
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
Example One
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!
Example Two
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.
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.
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
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
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
Contact Me!
Links● https://github.com/matthew-ch-robbins● https://au.linkedin.com/pub/matthew-
robbins/17/8a7/139
Email:● [email protected]