内存管理如何在 Xamarin.IOS 上工作 [英] How does memory management works on Xamarin.IOS

查看:15
本文介绍了内存管理如何在 Xamarin.IOS 上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解在使用 xamarin.ios 并在实际 iOS 设备上运行应用程序时内存管理的工作原理.我的理解是,iOS 平台没有垃圾收集器,但该平台使用 ARC(Automatci Reference Counting).

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

编译后的应用程序是否会使用 ARC 而不是垃圾收集?

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

推荐答案

ARC 是一种适用于 Objective-C 编译器编译的源代码的技术,它的作用是将每个赋值都变成这样:

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

其中foo"和bar"是NSObjects,代码如下:

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

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

如您所见,这只是一个编译器技巧,可以重写您的代码,因此您不会忘记保留/释放内容,并且仅适用于 Objective-C.

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.

Objective-C 库使用什么(ARC 或不使用 ARC)对于 MonoTouch 来说并不重要,因为它们使用现有的文档协议来确定何时保留和何时发布.MonoTouch 只是遵循这些规则.

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# 对象没有保留/释放代码路径,而只是使用 GC 来确定哪些对象是活动的.

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

当Objective-C 对象出现在C# 世界中时,Monotouch 接受一个引用(它调用retain).当 MonoTouch GC 确定任何托管代码都无法再访问某个​​对象时,GC 会对该对象调用 release.

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.

这篇关于内存管理如何在 Xamarin.IOS 上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆