Xamarin.android memory management gotchas

32
Xamarin.Android Memory Management Gotchas

Transcript of Xamarin.android memory management gotchas

Page 1: Xamarin.android memory management gotchas

Xamarin.Android Memory Management Gotchas

Page 2: 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

Page 3: Xamarin.android memory management gotchas

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...

Page 4: Xamarin.android memory management gotchas

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!

Page 5: Xamarin.android memory management gotchas

Xamarin.Android Memory Spaces

Reside only in Dalvik/ART space

Reside only in Mono space

Have object in Mono and Dalvik

Page 6: Xamarin.android memory management gotchas

Xamarin.Android Memory Spaces

Peer Objects

● 2 halves:

○ Managed

○ Native

● 2 types

○ User peers

○ Framework peers

Page 7: Xamarin.android memory management gotchas

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/

Page 8: Xamarin.android memory management gotchas
Page 9: 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!

Page 10: Xamarin.android memory management gotchas

Peer Objects

Managed Object

Page 11: Xamarin.android memory management gotchas

Peer Objects

Native Peer

Page 12: Xamarin.android memory management gotchas

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.

Page 13: Xamarin.android memory management gotchas

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

Page 14: Xamarin.android memory management gotchas

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

Page 15: Xamarin.android memory management gotchas

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.

Page 16: Xamarin.android memory management gotchas

Detecting Activity Leaks

Methods

● StrictMode

● Android Debug Bridge (ADB)

Page 17: Xamarin.android memory management gotchas

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

Page 18: Xamarin.android memory management gotchas

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())

Page 19: Xamarin.android memory management gotchas

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)

Page 20: Xamarin.android memory management gotchas

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

Page 21: Xamarin.android memory management gotchas

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!

Page 22: Xamarin.android memory management gotchas

Detecting Activity Leaks

Android Debug Bridge

Page 23: Xamarin.android memory management gotchas

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

Page 24: Xamarin.android memory management gotchas

Example One

Page 25: Xamarin.android memory management gotchas

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!

Page 26: Xamarin.android memory management gotchas

Example Two

Page 27: Xamarin.android memory management gotchas

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.

Page 28: Xamarin.android memory management gotchas

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.

Page 29: Xamarin.android memory management gotchas

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

Page 31: Xamarin.android memory management gotchas

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