使用mvvmCross帮助单声道机器人中的GC [英] Helping the GC in mono droid using mvvmCross

查看:111
本文介绍了使用mvvmCross帮助单声道机器人中的GC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用slodge提供的mvvmcross框架与单声道机器人合作。不过,我有一些内存问题。我将位图放置在ondestroy方法的活动中,我想知道是否可以帮助GC收集未使用的viewmodels对象。如果您尝试将活动中的viewmodel设置为null,则一切都将变为地狱,这显然不是正确的选择。



你们是否有任何建议方法?

问候

解决方案

mvx框架试图确保该活动拥有视图模型。



因此理论上,在你的活动被破坏之后,gc应该能够收集你所有的c#对象 - 活动,它拥有的视图,视图模型和它拥有的对象。



我发现这里出错了,任何'global'或singleton对象拥有对一个视图或视图模型对象。例如:


  • 如果一个视图向一个单例注册自己 - 例如一个http图像加载器 - 然后该单例保持一个引用如果一个viewmodel预订了一个中央服务(通常是一个singleton)上的事件并且不退订从它 - 然后在这种情况下,视图模型不能被垃圾收集(通常这也阻止了其他对象被收集)




通常,这两种类型的错误都可以通过对活动销毁执行清理操作来解决。但是,也可以使用其他方法 - 例如,对于事件订阅,您可以尝试使用弱引用(这也是在其他平台上采用的方法 - 例如mvvm light的使者)。

从经验,泄漏最明显的地方围绕像图像这样的大件物体 - 它们的尺寸可以帮助它们变得引人注目。然而,monodroid的真正挑战是确定泄漏的位置 - 修复它们通常比较容易。

遗憾的是,目前还没有可用于机器人的内存分析器。如果您正在交叉编译为wp7,那么当然对于视图模型对象/泄漏,您可以使用其内存分析器。如果不是这样,那么我通常试图解决内存泄漏的方式就是放大它们 - 尝试编写一个快速重现它们的示例 - 例如,通过将大字节[]成员添加到数据元素或通过快速重复操作。一旦你的泄漏容易复制,那么你可以尝试通过在终结器中放置跟踪语句,在事件删除处理程序等中找到泄漏。

I am working with mono droid, using the mvvmcross framework provided by slodge. However I am having some memory issues. I am disposing bitmaps in the activities ondestroy methods and I am wondering if it is possible to help the GC collecting unused objects of viewmodels. If you try setting the viewmodel in the activity to null it all goes to hell and it is clearly not the right way to go.

Do you guys have any suggestions to an approach?

Regards

解决方案

The mvx framework tries to ensure that the activity owns the viewmodel.

So in theory, after your activity has being destroyed, then the gc should be able to collect all of your c# objects - the activity, the views it owns, the view model and the objects it owns.

Where i've seen this this go wrong is where any 'global' or singleton object owns a reference to a view or viewmodel object. For example:

  • if a view registers itself with a singleton - eg an http image loader - and then that singleton keeps a reference to the view, preventing it from being garbage collected.

  • if a viewmodel subscribes to an event on a central service (often a singleton) and doesn't unsubscribe from it - then in this situation, the viewmodel can't be garbage collected (and often this also prevents other objects being collected too)

Generally both these types of errors can be solved by performing cleanup actions on activity destroy. However, other approaches are also available - eg for event subscriptions you can try using weak references (this is an approach taken on other platforms too - eg mvvm light's messenger)

From experience, the areas where leaks are most noticeable are around 'big objects' like images - their size helps them become noticeable. However, the real challenge on monodroid is identifying where the leaks are - fixing them is generally comparatively easy.

Sadly, there isn't currently a memory profiler available for droid. If you are cross-compiling to wp7, then certainly for viewmodel objects/leaks you can use its memory profiler. If not, then the way I generally try to solve memory leaks is to amplify them - try writing a sample that rapidly reproduces them - eg by adding large byte[] members to data elements or by rapidly repeating actions. Once you have the leak easily reproduced, then you can try to find the leaks by placing trace statements in finalizers, in event remove handlers, etc.

这篇关于使用mvvmCross帮助单声道机器人中的GC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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