SlideShare a Scribd company logo
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/
Xamarin.android memory management gotchas
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:
● matthew.ch.robbins@gmail.com
Ad

More Related Content

What's hot (15)

Common mistakes in android development
Common mistakes in android developmentCommon mistakes in android development
Common mistakes in android development
Hoang Nguyen Huu
 
Memory management in iOS.
Memory management in iOS.Memory management in iOS.
Memory management in iOS.
HSIEH CHING-FAN
 
Memory management in Objective C
Memory management in Objective CMemory management in Objective C
Memory management in Objective C
Neha Gupta
 
GC in C#
GC in C#GC in C#
GC in C#
Kamal1997
 
Extending Node.js using C++
Extending Node.js using C++Extending Node.js using C++
Extending Node.js using C++
Kenneth Geisshirt
 
Distributing information on iOS
Distributing information on iOSDistributing information on iOS
Distributing information on iOS
Make School
 
Mobile Developers Talks: Delve Mobile
Mobile Developers Talks: Delve MobileMobile Developers Talks: Delve Mobile
Mobile Developers Talks: Delve Mobile
Konstantin Loginov
 
iOS Memory management & Navigation
iOS Memory management & NavigationiOS Memory management & Navigation
iOS Memory management & Navigation
Marian Ignev
 
Objective C Memory Management
Objective C Memory ManagementObjective C Memory Management
Objective C Memory Management
Ahmed Magdy Ezzeldin, MSc.
 
EventBus for Android
EventBus for AndroidEventBus for Android
EventBus for Android
greenrobot
 
JavaScript and UI Architecture Best Practices
JavaScript and UI Architecture Best PracticesJavaScript and UI Architecture Best Practices
JavaScript and UI Architecture Best Practices
Siarhei Barysiuk
 
UIKit Dynamics
UIKit DynamicsUIKit Dynamics
UIKit Dynamics
Craig VanderZwaag
 
Even more java script best practices
Even more java script best practicesEven more java script best practices
Even more java script best practices
ChengHui Weng
 
Guide to Destroying Codebases The Demise of Clever Code
Guide to Destroying Codebases   The Demise of Clever CodeGuide to Destroying Codebases   The Demise of Clever Code
Guide to Destroying Codebases The Demise of Clever Code
Gabor Varadi
 
【Potatotips #26】Replace EventBus with RxJava/RxAndroid
【Potatotips #26】Replace EventBus with RxJava/RxAndroid【Potatotips #26】Replace EventBus with RxJava/RxAndroid
【Potatotips #26】Replace EventBus with RxJava/RxAndroid
Hiroyuki Kusu
 
Common mistakes in android development
Common mistakes in android developmentCommon mistakes in android development
Common mistakes in android development
Hoang Nguyen Huu
 
Memory management in iOS.
Memory management in iOS.Memory management in iOS.
Memory management in iOS.
HSIEH CHING-FAN
 
Memory management in Objective C
Memory management in Objective CMemory management in Objective C
Memory management in Objective C
Neha Gupta
 
Distributing information on iOS
Distributing information on iOSDistributing information on iOS
Distributing information on iOS
Make School
 
Mobile Developers Talks: Delve Mobile
Mobile Developers Talks: Delve MobileMobile Developers Talks: Delve Mobile
Mobile Developers Talks: Delve Mobile
Konstantin Loginov
 
iOS Memory management & Navigation
iOS Memory management & NavigationiOS Memory management & Navigation
iOS Memory management & Navigation
Marian Ignev
 
EventBus for Android
EventBus for AndroidEventBus for Android
EventBus for Android
greenrobot
 
JavaScript and UI Architecture Best Practices
JavaScript and UI Architecture Best PracticesJavaScript and UI Architecture Best Practices
JavaScript and UI Architecture Best Practices
Siarhei Barysiuk
 
Even more java script best practices
Even more java script best practicesEven more java script best practices
Even more java script best practices
ChengHui Weng
 
Guide to Destroying Codebases The Demise of Clever Code
Guide to Destroying Codebases   The Demise of Clever CodeGuide to Destroying Codebases   The Demise of Clever Code
Guide to Destroying Codebases The Demise of Clever Code
Gabor Varadi
 
【Potatotips #26】Replace EventBus with RxJava/RxAndroid
【Potatotips #26】Replace EventBus with RxJava/RxAndroid【Potatotips #26】Replace EventBus with RxJava/RxAndroid
【Potatotips #26】Replace EventBus with RxJava/RxAndroid
Hiroyuki Kusu
 

Viewers also liked (10)

Vietnam Mobile Day 2013: Memory Management For Android Apps
Vietnam Mobile Day 2013: Memory Management For Android AppsVietnam Mobile Day 2013: Memory Management For Android Apps
Vietnam Mobile Day 2013: Memory Management For Android Apps
GameLandVN
 
Memory in Android
Memory in AndroidMemory in Android
Memory in Android
Sergey Bandysik
 
Memory management in Android
Memory management in AndroidMemory management in Android
Memory management in Android
Keyhan Asghari
 
Memory Management in Android
Memory Management in AndroidMemory Management in Android
Memory Management in Android
Opersys inc.
 
Android & IOS
Android & IOSAndroid & IOS
Android & IOS
Arpee Callejo
 
Memory management in Andoid
Memory management in AndoidMemory management in Andoid
Memory management in Andoid
Monkop Inc
 
Gc in android
Gc in androidGc in android
Gc in android
Vikas Balikai
 
Android memory fundamentals
Android memory fundamentalsAndroid memory fundamentals
Android memory fundamentals
Taras Leskiv
 
Android - Preventing common memory leaks
Android - Preventing common memory leaksAndroid - Preventing common memory leaks
Android - Preventing common memory leaks
Ali Muzaffar
 
Apple iOS Introduction
Apple iOS IntroductionApple iOS Introduction
Apple iOS Introduction
Pratik Vyas
 
Vietnam Mobile Day 2013: Memory Management For Android Apps
Vietnam Mobile Day 2013: Memory Management For Android AppsVietnam Mobile Day 2013: Memory Management For Android Apps
Vietnam Mobile Day 2013: Memory Management For Android Apps
GameLandVN
 
Memory management in Android
Memory management in AndroidMemory management in Android
Memory management in Android
Keyhan Asghari
 
Memory Management in Android
Memory Management in AndroidMemory Management in Android
Memory Management in Android
Opersys inc.
 
Memory management in Andoid
Memory management in AndoidMemory management in Andoid
Memory management in Andoid
Monkop Inc
 
Android memory fundamentals
Android memory fundamentalsAndroid memory fundamentals
Android memory fundamentals
Taras Leskiv
 
Android - Preventing common memory leaks
Android - Preventing common memory leaksAndroid - Preventing common memory leaks
Android - Preventing common memory leaks
Ali Muzaffar
 
Apple iOS Introduction
Apple iOS IntroductionApple iOS Introduction
Apple iOS Introduction
Pratik Vyas
 
Ad

Similar to Xamarin.android memory management gotchas (20)

10 ways to improve your Android app performance
10 ways to improve your Android app performance10 ways to improve your Android app performance
10 ways to improve your Android app performance
Boris Farber
 
Headless Android
Headless AndroidHeadless Android
Headless Android
Opersys inc.
 
Memory Leaks in Android Applications
Memory Leaks in Android ApplicationsMemory Leaks in Android Applications
Memory Leaks in Android Applications
Lokesh Ponnada
 
Leveraging Android's Linux Heritage at AnDevCon3
Leveraging Android's Linux Heritage at AnDevCon3Leveraging Android's Linux Heritage at AnDevCon3
Leveraging Android's Linux Heritage at AnDevCon3
Opersys inc.
 
Android Platform Debugging and Development
Android Platform Debugging and DevelopmentAndroid Platform Debugging and Development
Android Platform Debugging and Development
Opersys inc.
 
Begining Android Development
Begining Android DevelopmentBegining Android Development
Begining Android Development
Hayi Nukman
 
Android Jump Start
Android Jump StartAndroid Jump Start
Android Jump Start
ConFoo
 
Android Platform Debugging and Development
Android Platform Debugging and DevelopmentAndroid Platform Debugging and Development
Android Platform Debugging and Development
Opersys inc.
 
Headless Android at AnDevCon3
Headless Android at AnDevCon3Headless Android at AnDevCon3
Headless Android at AnDevCon3
Opersys inc.
 
Leveraging Android's Linux Heritage
Leveraging Android's Linux HeritageLeveraging Android's Linux Heritage
Leveraging Android's Linux Heritage
Opersys inc.
 
The art of android hacking by Abhinav Mishra (0ctac0der)
The art of  android hacking by Abhinav Mishra (0ctac0der)The art of  android hacking by Abhinav Mishra (0ctac0der)
The art of android hacking by Abhinav Mishra (0ctac0der)
OWASP Delhi
 
The art of android hacking
The art of  android hackingThe art of  android hacking
The art of android hacking
Abhinav Mishra
 
Fast mobile web apps
Fast mobile web appsFast mobile web apps
Fast mobile web apps
Ivano Malavolta
 
Android Platform Debugging and Development
Android Platform Debugging and DevelopmentAndroid Platform Debugging and Development
Android Platform Debugging and Development
Opersys inc.
 
Android Platform Debugging and Development
Android Platform Debugging and DevelopmentAndroid Platform Debugging and Development
Android Platform Debugging and Development
Karim Yaghmour
 
Android Platform Debugging and Development
Android Platform Debugging and DevelopmentAndroid Platform Debugging and Development
Android Platform Debugging and Development
Opersys inc.
 
Leakcanary tool
Leakcanary toolLeakcanary tool
Leakcanary tool
孝庭 陳
 
Android Platform Debugging and Development
Android Platform Debugging and DevelopmentAndroid Platform Debugging and Development
Android Platform Debugging and Development
Opersys inc.
 
Android Platform Debugging and Development
Android Platform Debugging and DevelopmentAndroid Platform Debugging and Development
Android Platform Debugging and Development
Opersys inc.
 
An Abusive Relationship with AngularJS
An Abusive Relationship with AngularJSAn Abusive Relationship with AngularJS
An Abusive Relationship with AngularJS
Mario Heiderich
 
10 ways to improve your Android app performance
10 ways to improve your Android app performance10 ways to improve your Android app performance
10 ways to improve your Android app performance
Boris Farber
 
Memory Leaks in Android Applications
Memory Leaks in Android ApplicationsMemory Leaks in Android Applications
Memory Leaks in Android Applications
Lokesh Ponnada
 
Leveraging Android's Linux Heritage at AnDevCon3
Leveraging Android's Linux Heritage at AnDevCon3Leveraging Android's Linux Heritage at AnDevCon3
Leveraging Android's Linux Heritage at AnDevCon3
Opersys inc.
 
Android Platform Debugging and Development
Android Platform Debugging and DevelopmentAndroid Platform Debugging and Development
Android Platform Debugging and Development
Opersys inc.
 
Begining Android Development
Begining Android DevelopmentBegining Android Development
Begining Android Development
Hayi Nukman
 
Android Jump Start
Android Jump StartAndroid Jump Start
Android Jump Start
ConFoo
 
Android Platform Debugging and Development
Android Platform Debugging and DevelopmentAndroid Platform Debugging and Development
Android Platform Debugging and Development
Opersys inc.
 
Headless Android at AnDevCon3
Headless Android at AnDevCon3Headless Android at AnDevCon3
Headless Android at AnDevCon3
Opersys inc.
 
Leveraging Android's Linux Heritage
Leveraging Android's Linux HeritageLeveraging Android's Linux Heritage
Leveraging Android's Linux Heritage
Opersys inc.
 
The art of android hacking by Abhinav Mishra (0ctac0der)
The art of  android hacking by Abhinav Mishra (0ctac0der)The art of  android hacking by Abhinav Mishra (0ctac0der)
The art of android hacking by Abhinav Mishra (0ctac0der)
OWASP Delhi
 
The art of android hacking
The art of  android hackingThe art of  android hacking
The art of android hacking
Abhinav Mishra
 
Android Platform Debugging and Development
Android Platform Debugging and DevelopmentAndroid Platform Debugging and Development
Android Platform Debugging and Development
Opersys inc.
 
Android Platform Debugging and Development
Android Platform Debugging and DevelopmentAndroid Platform Debugging and Development
Android Platform Debugging and Development
Karim Yaghmour
 
Android Platform Debugging and Development
Android Platform Debugging and DevelopmentAndroid Platform Debugging and Development
Android Platform Debugging and Development
Opersys inc.
 
Leakcanary tool
Leakcanary toolLeakcanary tool
Leakcanary tool
孝庭 陳
 
Android Platform Debugging and Development
Android Platform Debugging and DevelopmentAndroid Platform Debugging and Development
Android Platform Debugging and Development
Opersys inc.
 
Android Platform Debugging and Development
Android Platform Debugging and DevelopmentAndroid Platform Debugging and Development
Android Platform Debugging and Development
Opersys inc.
 
An Abusive Relationship with AngularJS
An Abusive Relationship with AngularJSAn Abusive Relationship with AngularJS
An Abusive Relationship with AngularJS
Mario Heiderich
 
Ad

More from Alec Tucker (18)

Monkey fest australia 2020
Monkey fest australia 2020Monkey fest australia 2020
Monkey fest australia 2020
Alec Tucker
 
Enterprise Mobile Security and OWASP Compliance
Enterprise Mobile Security and OWASP ComplianceEnterprise Mobile Security and OWASP Compliance
Enterprise Mobile Security and OWASP Compliance
Alec Tucker
 
Addressing the OWASP Mobile Security Threats using Xamarin
Addressing the OWASP Mobile Security Threats using XamarinAddressing the OWASP Mobile Security Threats using Xamarin
Addressing the OWASP Mobile Security Threats using Xamarin
Alec Tucker
 
Sydney Mobile .Net (Xamarin) Developers Group March 2016
Sydney Mobile .Net (Xamarin) Developers Group March 2016Sydney Mobile .Net (Xamarin) Developers Group March 2016
Sydney Mobile .Net (Xamarin) Developers Group March 2016
Alec Tucker
 
SydMobNet March 2016: Matthew Robbins - Android M Security Policies
SydMobNet March 2016: Matthew Robbins - Android M Security PoliciesSydMobNet March 2016: Matthew Robbins - Android M Security Policies
SydMobNet March 2016: Matthew Robbins - Android M Security Policies
Alec Tucker
 
Sydney Mobile .Net (Xamarin) Developers Group January 2016
Sydney Mobile .Net (Xamarin) Developers Group January 2016Sydney Mobile .Net (Xamarin) Developers Group January 2016
Sydney Mobile .Net (Xamarin) Developers Group January 2016
Alec Tucker
 
Sydney Mobile .Net Developers Group February 2015
Sydney Mobile .Net Developers Group February 2015Sydney Mobile .Net Developers Group February 2015
Sydney Mobile .Net Developers Group February 2015
Alec Tucker
 
Sydney Mobile .Net Developers Group January 2015
Sydney Mobile .Net Developers Group January 2015Sydney Mobile .Net Developers Group January 2015
Sydney Mobile .Net Developers Group January 2015
Alec Tucker
 
Sydney Mobile .Net Developers Group December 2014
Sydney Mobile .Net Developers Group December 2014Sydney Mobile .Net Developers Group December 2014
Sydney Mobile .Net Developers Group December 2014
Alec Tucker
 
#SydMobNet Nov 2014: Evolve 2014 recap
#SydMobNet Nov 2014: Evolve 2014 recap#SydMobNet Nov 2014: Evolve 2014 recap
#SydMobNet Nov 2014: Evolve 2014 recap
Alec Tucker
 
Sydney Mobile .Net Developers Group November 2014
Sydney Mobile .Net Developers Group November 2014Sydney Mobile .Net Developers Group November 2014
Sydney Mobile .Net Developers Group November 2014
Alec Tucker
 
SydMobNet September 2014: ReactiveUI, Genymotion, Xamarin.UITest and Xamarin ...
SydMobNet September 2014: ReactiveUI, Genymotion, Xamarin.UITest and Xamarin ...SydMobNet September 2014: ReactiveUI, Genymotion, Xamarin.UITest and Xamarin ...
SydMobNet September 2014: ReactiveUI, Genymotion, Xamarin.UITest and Xamarin ...
Alec Tucker
 
SydMobNet August 2014: What's New in iOS8 & Xamarin plus .Net MVC and Xamarin...
SydMobNet August 2014: What's New in iOS8 & Xamarin plus .Net MVC and Xamarin...SydMobNet August 2014: What's New in iOS8 & Xamarin plus .Net MVC and Xamarin...
SydMobNet August 2014: What's New in iOS8 & Xamarin plus .Net MVC and Xamarin...
Alec Tucker
 
SydMobNet July 2014: Xamarin 3 & Xamarin Forms
SydMobNet July 2014: Xamarin 3 & Xamarin FormsSydMobNet July 2014: Xamarin 3 & Xamarin Forms
SydMobNet July 2014: Xamarin 3 & Xamarin Forms
Alec Tucker
 
SydMobNet May 2014 - Lewis Benge on Wearable Tech
SydMobNet May 2014 - Lewis Benge on Wearable TechSydMobNet May 2014 - Lewis Benge on Wearable Tech
SydMobNet May 2014 - Lewis Benge on Wearable Tech
Alec Tucker
 
SydMobNet April 2014 - Nick Randolph's Build 2014 Update
SydMobNet April 2014 - Nick Randolph's Build 2014 UpdateSydMobNet April 2014 - Nick Randolph's Build 2014 Update
SydMobNet April 2014 - Nick Randolph's Build 2014 Update
Alec Tucker
 
Internet of Things, Mobility & .Net Micro Framework SydMobNet March 2014
Internet of Things, Mobility & .Net Micro Framework SydMobNet March 2014Internet of Things, Mobility & .Net Micro Framework SydMobNet March 2014
Internet of Things, Mobility & .Net Micro Framework SydMobNet March 2014
Alec Tucker
 
SydMobDev Feb 2014 - Cross Platform Native App Development with Xamarin and M...
SydMobDev Feb 2014 - Cross Platform Native App Development with Xamarin and M...SydMobDev Feb 2014 - Cross Platform Native App Development with Xamarin and M...
SydMobDev Feb 2014 - Cross Platform Native App Development with Xamarin and M...
Alec Tucker
 
Monkey fest australia 2020
Monkey fest australia 2020Monkey fest australia 2020
Monkey fest australia 2020
Alec Tucker
 
Enterprise Mobile Security and OWASP Compliance
Enterprise Mobile Security and OWASP ComplianceEnterprise Mobile Security and OWASP Compliance
Enterprise Mobile Security and OWASP Compliance
Alec Tucker
 
Addressing the OWASP Mobile Security Threats using Xamarin
Addressing the OWASP Mobile Security Threats using XamarinAddressing the OWASP Mobile Security Threats using Xamarin
Addressing the OWASP Mobile Security Threats using Xamarin
Alec Tucker
 
Sydney Mobile .Net (Xamarin) Developers Group March 2016
Sydney Mobile .Net (Xamarin) Developers Group March 2016Sydney Mobile .Net (Xamarin) Developers Group March 2016
Sydney Mobile .Net (Xamarin) Developers Group March 2016
Alec Tucker
 
SydMobNet March 2016: Matthew Robbins - Android M Security Policies
SydMobNet March 2016: Matthew Robbins - Android M Security PoliciesSydMobNet March 2016: Matthew Robbins - Android M Security Policies
SydMobNet March 2016: Matthew Robbins - Android M Security Policies
Alec Tucker
 
Sydney Mobile .Net (Xamarin) Developers Group January 2016
Sydney Mobile .Net (Xamarin) Developers Group January 2016Sydney Mobile .Net (Xamarin) Developers Group January 2016
Sydney Mobile .Net (Xamarin) Developers Group January 2016
Alec Tucker
 
Sydney Mobile .Net Developers Group February 2015
Sydney Mobile .Net Developers Group February 2015Sydney Mobile .Net Developers Group February 2015
Sydney Mobile .Net Developers Group February 2015
Alec Tucker
 
Sydney Mobile .Net Developers Group January 2015
Sydney Mobile .Net Developers Group January 2015Sydney Mobile .Net Developers Group January 2015
Sydney Mobile .Net Developers Group January 2015
Alec Tucker
 
Sydney Mobile .Net Developers Group December 2014
Sydney Mobile .Net Developers Group December 2014Sydney Mobile .Net Developers Group December 2014
Sydney Mobile .Net Developers Group December 2014
Alec Tucker
 
#SydMobNet Nov 2014: Evolve 2014 recap
#SydMobNet Nov 2014: Evolve 2014 recap#SydMobNet Nov 2014: Evolve 2014 recap
#SydMobNet Nov 2014: Evolve 2014 recap
Alec Tucker
 
Sydney Mobile .Net Developers Group November 2014
Sydney Mobile .Net Developers Group November 2014Sydney Mobile .Net Developers Group November 2014
Sydney Mobile .Net Developers Group November 2014
Alec Tucker
 
SydMobNet September 2014: ReactiveUI, Genymotion, Xamarin.UITest and Xamarin ...
SydMobNet September 2014: ReactiveUI, Genymotion, Xamarin.UITest and Xamarin ...SydMobNet September 2014: ReactiveUI, Genymotion, Xamarin.UITest and Xamarin ...
SydMobNet September 2014: ReactiveUI, Genymotion, Xamarin.UITest and Xamarin ...
Alec Tucker
 
SydMobNet August 2014: What's New in iOS8 & Xamarin plus .Net MVC and Xamarin...
SydMobNet August 2014: What's New in iOS8 & Xamarin plus .Net MVC and Xamarin...SydMobNet August 2014: What's New in iOS8 & Xamarin plus .Net MVC and Xamarin...
SydMobNet August 2014: What's New in iOS8 & Xamarin plus .Net MVC and Xamarin...
Alec Tucker
 
SydMobNet July 2014: Xamarin 3 & Xamarin Forms
SydMobNet July 2014: Xamarin 3 & Xamarin FormsSydMobNet July 2014: Xamarin 3 & Xamarin Forms
SydMobNet July 2014: Xamarin 3 & Xamarin Forms
Alec Tucker
 
SydMobNet May 2014 - Lewis Benge on Wearable Tech
SydMobNet May 2014 - Lewis Benge on Wearable TechSydMobNet May 2014 - Lewis Benge on Wearable Tech
SydMobNet May 2014 - Lewis Benge on Wearable Tech
Alec Tucker
 
SydMobNet April 2014 - Nick Randolph's Build 2014 Update
SydMobNet April 2014 - Nick Randolph's Build 2014 UpdateSydMobNet April 2014 - Nick Randolph's Build 2014 Update
SydMobNet April 2014 - Nick Randolph's Build 2014 Update
Alec Tucker
 
Internet of Things, Mobility & .Net Micro Framework SydMobNet March 2014
Internet of Things, Mobility & .Net Micro Framework SydMobNet March 2014Internet of Things, Mobility & .Net Micro Framework SydMobNet March 2014
Internet of Things, Mobility & .Net Micro Framework SydMobNet March 2014
Alec Tucker
 
SydMobDev Feb 2014 - Cross Platform Native App Development with Xamarin and M...
SydMobDev Feb 2014 - Cross Platform Native App Development with Xamarin and M...SydMobDev Feb 2014 - Cross Platform Native App Development with Xamarin and M...
SydMobDev Feb 2014 - Cross Platform Native App Development with Xamarin and M...
Alec Tucker
 

Xamarin.android memory management gotchas

  • 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. 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. 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. Xamarin.Android Memory Spaces Reside only in Dalvik/ART space Reside only in Mono space Have object in Mono and Dalvik
  • 6. Xamarin.Android Memory Spaces Peer Objects ● 2 halves: ○ Managed ○ Native ● 2 types ○ User peers ○ Framework peers
  • 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/
  • 9. 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!
  • 12. 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.
  • 13. 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
  • 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) ■ Start activities
  • 15. 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.
  • 16. Detecting Activity Leaks Methods ● StrictMode ● Android Debug Bridge (ADB)
  • 17. 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
  • 18. 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())
  • 19. 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)
  • 20. 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
  • 21. 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!
  • 23. 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
  • 25. 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!
  • 27. 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.
  • 28. 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.
  • 29. 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
  • 30. 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
  • 31. 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
  • 32. Contact Me! Links ● https://github.com/matthew-ch-robbins ● https://au.linkedin.com/pub/matthew- robbins/17/8a7/139 Email: ● [email protected]