Transcript
Page 1: Xamarin - Under the bridge

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals @ITCAMPRO #ITCAMP17Community Conference for IT Professionals

Xamarin - Under the Bridge

Microsoft&Xamarin MVP, Xamarin Certified Mobile Developer

CTO Mahiz S.r.l.

[email protected] Twitter: @danardelean

Dan Ardelean

Page 2: Xamarin - Under the bridge

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals @ITCAMPRO #ITCAMP17Community Conference for IT Professionals

Many thanks to our sponsors & partners!

GOLD

SILVER

PARTNERS

PLATINUM

POWERED BY

Page 3: Xamarin - Under the bridge

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals @ITCAMPRO #ITCAMP17Community Conference for IT Professionals

XAMARIN

Page 4: Xamarin - Under the bridge

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals @ITCAMPRO #ITCAMP17Community Conference for IT Professionals

XAMARIN

C#/F#

Visual Studio

Xamarin Studio

Black Magic

Native

Page 5: Xamarin - Under the bridge

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals @ITCAMPRO #ITCAMP17Community Conference for IT Professionals

iOS Native Applications

Page 6: Xamarin - Under the bridge

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals @ITCAMPRO #ITCAMP17Community Conference for IT Professionals

iOS Native Application

Page 7: Xamarin - Under the bridge

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals @ITCAMPRO #ITCAMP17Community Conference for IT Professionals

DEMO XCODE

SINGLE VIEW APPLICATION

Page 8: Xamarin - Under the bridge

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals @ITCAMPRO #ITCAMP17Community Conference for IT Professionals

C# to Objective-C world

Page 9: Xamarin - Under the bridge

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals @ITCAMPRO #ITCAMP17Community Conference for IT Professionals

Xamarin iOS Compilation

Page 10: Xamarin - Under the bridge

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals @ITCAMPRO #ITCAMP17Community Conference for IT Professionals

• Expose Objectve-C to C# using Selectors• objc_msgSend functions

Selectors

-[NSString sizeWithFont:forWidth:lineBreakMode:]

sizeWithFont Selector:

- (CGSize)sizeWithFont:(UIFont *)font forWidth:(CGFloat)width lineBreakMode:(UILineBreakMode)lineBreakMode

Objective-C declaration:

CGSize objc_msgSend(IntPtr target, IntPtr selector, IntPtr font, nfloat width, UILineBreakMode mode);

objc_msgSend

[DllImport (Constants.ObjectiveCLibrary,EntryPoint="objc_msgSend")]

static extern CGSize cgsize_objc_msgSend_IntPtr_float_int ( IntPtr target, IntPtr selector, IntPtr font,

nfloat width, UILineBreakMode mode);

Page 11: Xamarin - Under the bridge

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals @ITCAMPRO #ITCAMP17Community Conference for IT Professionals

• Expose Objectve-C to C# using Selectors• objc_msgSend functions

Selectors

NSString target = ...

Selector selector = new Selector ("sizeWithFont:forWidth:lineBreakMode:");

UIFont font = ...

nfloat width = ...

UILineBreakMode mode = ...

CGSize size = cgsize_objc_msgSend_IntPtr_float_int( target.Handle, selector.Handle, font == null ?

IntPtr.Zero : font.Handle, width, mode);

Page 12: Xamarin - Under the bridge

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals @ITCAMPRO #ITCAMP17Community Conference for IT Professionals

• Expose Managed Code to Objective-C

Registrars

[Register]

class MyViewController : UIViewController{

[Export ("myFunc")]

public void MyFunc () { }

}

C# (Managed Code)

Objective-C

@interface MyViewController : UIViewController { } -(void)myFunc;

@end @implementation

MyViewController {} -(void) myFunc{ /* code to call the managed MyViewController.MyFunc method */}

@end

Page 13: Xamarin - Under the bridge

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals @ITCAMPRO #ITCAMP17Community Conference for IT Professionals

DEMO XAMARIN IOS

Single View Application

Page 14: Xamarin - Under the bridge

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals @ITCAMPRO #ITCAMP17Community Conference for IT Professionals

Xamarin.iOS creates a managed wrapper, called a peer

object, for every native object accessed by the

Xamarin.iOS runtime

Peer wrapper objects

UIKit.UIButton

(peer object)

[UIButton]

(native object)IntPtrHandle

Allocated by

Xamarin.iOS

and collected by the

GC

Allocated by the iOS

runtime

and reference counted

Page 15: Xamarin - Under the bridge

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals @ITCAMPRO #ITCAMP17Community Conference for IT Professionals

• Peer object increment the retain count; it is released

when the managed peer is disposed or finalized

Memory allocation Xamarin.iOS

1

GC

Collect[brelease] 0

b =null;

b.Dispose(); [brelease] 0

var b = new UIButton(...) [[UIButton alloc] init]

or

Page 16: Xamarin - Under the bridge

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals @ITCAMPRO #ITCAMP17Community Conference for IT Professionals

Monitoring memory allocations

Memory

Analysis

(Heap shot)

Instrument

s

Androi

d

Device

Monitor

Xamarin

Profiler

VS

Profiler

Page 17: Xamarin - Under the bridge

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals @ITCAMPRO #ITCAMP17Community Conference for IT Professionals

DEMO MEMORY ALLOCATION

Single View Application

Page 18: Xamarin - Under the bridge

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals @ITCAMPRO #ITCAMP17Community Conference for IT Professionals

Prefer full delegate methods over lambdas – it makes it

easier to see and understand strong references

Call Dispose() to release native resources immediately (vs.

waiting ona GC) when you are finished with a peer

wrapper

Always unsubscribe from events you manually wire up;

alternatively, use the Storyboard to connect events which

can then be cleaned up automatically

Xamarin.iOS Tips

Page 19: Xamarin - Under the bridge

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals @ITCAMPRO #ITCAMP17Community Conference for IT Professionals

Android native application

Page 20: Xamarin - Under the bridge

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals @ITCAMPRO #ITCAMP17Community Conference for IT Professionals

Android Native Compilation

.dexfilesJava

compile

r

Android

dex

compiler

Java

bytecodes

Dalvik

bytecodes

Page 21: Xamarin - Under the bridge

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals @ITCAMPRO #ITCAMP17Community Conference for IT Professionals

Android Native Compilation

.dex files

Resources

(e.g.

images)

.apkfileAndroid

apkbuilde

rThe .apkfile contains

all the app's assets

and is ready to run

Page 22: Xamarin - Under the bridge

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals @ITCAMPRO #ITCAMP17Community Conference for IT Professionals

Android Native Execution

Bytecodes are compiled to

native code at installation

(called Ahead-of-Time or

AOT)

MyApp.apkAndroid

Runtime

(ART)

Apps run in their own

process with their own

copy of the ART Virtual

Machine

Page 23: Xamarin - Under the bridge

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals @ITCAMPRO #ITCAMP17Community Conference for IT Professionals

DEMO ANDROID STUDIO

Page 24: Xamarin - Under the bridge

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals @ITCAMPRO #ITCAMP17Community Conference for IT Professionals

Xamarin.Android Architecture

Page 25: Xamarin - Under the bridge

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals @ITCAMPRO #ITCAMP17Community Conference for IT Professionals

Xamarin.Android Compilation

assemblyC#

compiler

IL and

metadata

Linke

r

Monoassemblies

yourassemblies

Monoassemblies

(filtered)

Determines which class members are used in

your app and includes only those members in

the output

yourassemblies

(filtered)

Page 26: Xamarin - Under the bridge

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals @ITCAMPRO #ITCAMP17Community Conference for IT Professionals

Xamarin.Android Compilation

Mono

Runtime

The Xamarin build tools

add the Mono VM to

your application

package

.apkfileAndroid

apkbuilde

r

Page 27: Xamarin - Under the bridge

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals @ITCAMPRO #ITCAMP17Community Conference for IT Professionals

DEMO XAMARIN.ANDROID

Page 28: Xamarin - Under the bridge

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals @ITCAMPRO #ITCAMP17Community Conference for IT Professionals

Xamarin.Android GC Process

GC

Collect

Can

collect

object?

Is

Peer

Object

?

Ye

sReplace JNI

handle with

Weak Handle

Ye

s

Force Android GCJNI

handle

collected

?

Collect objectNo

Ye

s

Replace JNI

weak handle

with strong

handle reference

NoPeer

object

kept alive

Page 29: Xamarin - Under the bridge

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals @ITCAMPRO #ITCAMP17Community Conference for IT Professionals

• Integration between Xamarin GC and JVM GC is

performed through a native extension called the GC

Bridge

GC Bridge

Xamarin

Manage

d

Memory

Android

Manage

d

Memory

GC

Bridge

Process

Memor

y

Page 30: Xamarin - Under the bridge

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals @ITCAMPRO #ITCAMP17Community Conference for IT Professionals

• GC_ messages provide details about bridge and

collection times

Monitoring GC Performance

GC_MAJOR: (Minor allowance) pause 29.11ms, total 29.35ms, bridge 0.15msmajor

4048K/0K los 2766K/0K

Lower pause, bridge and total times are

preferred

Page 31: Xamarin - Under the bridge

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals @ITCAMPRO #ITCAMP17Community Conference for IT Professionals

• C# objects must be boxed to create a JVM

representation of the object; intrinsic types (strings,

numeric values and dates) are all special cased

Xamarin and the JAVA VM

C#

ObjectJava

Object

ArrayAdapter<Tweet>ArrayAdapter

ArrayLi

stTweet Tweet Tweet Tweet

Tweet Tweet Tweet Tweet

ListView

Page 32: Xamarin - Under the bridge

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals @ITCAMPRO #ITCAMP17Community Conference for IT Professionals

• Instead, do as much as possible in either C# or Java,

interop is expensive

Stay in your yard

C# Object

Java

Object

TweetAdapter

List<Tweet>

Tweet Tweet Tweet Tweet

ListView

Page 33: Xamarin - Under the bridge

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals @ITCAMPRO #ITCAMP17Community Conference for IT Professionals

Should call Dispose() to release the native resources

immediately (vs waiting on a GC) when you are finished

with a peer wrapper

Avoid placing a large numbers of references in peer

objects, remember GC is more expensive for these special

types

Avoid passing pure C# custom types into Java APIs if

possible

Xamarin.Android Tips

Page 34: Xamarin - Under the bridge

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals @ITCAMPRO #ITCAMP17Community Conference for IT Professionals

Xamarin

Process

Memory

UIView

UIImage

MKMapVie

w

...

Activity

Intent

TextView

...

Managed

Memory

C# and .NET

objects are

allocated here

- this is all GC

knows about

Page 35: Xamarin - Under the bridge

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals @ITCAMPRO #ITCAMP17Community Conference for IT Professionals

Q & A