18

I am trying understand how memory management works when using xamarin.ios and running the app on an actual iOS device. My understanding that the iOS platform does not have a garbage collector, but the platform uses ARC (Automatci Reference Counting).

Is it true that the compiled app will be using ARC instead of garbage collection?

gyurisc
  • 11,234
  • 16
  • 68
  • 102
  • A good presentation on the topic: "Advanced Memory Management on iOS and Android - Evolve 2013" http://xamarin.com/evolve/2013#session-0w86u7bco2 – James Moore Aug 18 '14 at 16:08

2 Answers2

33

ARC is a technology that applies to source code compiled by the Objective-C compiler and it has the effect of turning every assignment like this:

foo = bar

Where "foo" and "bar" are NSObjects into the following code:

if (foo != null)
   [foo release];
if (bar != null)
   [bar retain]
foo = bar;

As you can see, it is just a compiler trick that rewrites your code so you do not forget to retain/release things and only applies to Objective-C.

What Objective-C libraries use (ARC or no ARC) is not really important to MonoTouch, as far as they use the existing documented protocol for when to retain and when to release. MonoTouch just follows those rules.

C# objects do not have the retain/release code path, and instead just use GC to determine which objects are alive.

When Objective-C objects are surfaced to the C# world, Monotouch takes a reference (it calls retain). When the MonoTouch GC determines that an object is no longer reachable by any managed code, then the GC calls release on the object.

miguel.de.icaza
  • 32,654
  • 6
  • 58
  • 76
  • 1
    **When Objective-C objects are surfaced to the C# world, Monotouch takes a reference (it calls retain). When the MonoTouch GC determines that an object is no longer reachable by any managed code, then the GC calls release on the object.** I am confused with this.. In Xamarin.ios we use only C# no Objective C ... you mean to say for everything we create in C# monotouch creates an equivalent thing using Objective C...if you could shed some light on this ,it'd be good – Durai Amuthan.H Feb 09 '17 at 12:54
7

There is a great summarisation in the Xamarin docs Here

Blounty
  • 3,342
  • 22
  • 21