单声道为Android - 的OutOfMemoryError [英] Mono for Android - OutOfMemoryError

查看:98
本文介绍了单声道为Android - 的OutOfMemoryError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得为Android应用程序在我的单声道内存泄漏问题。我相信我下面所有的最佳实践,以后说明,但我一直活动的运行得来的一致,可重复的次数后得到的OutOfMemoryError。

I am having problems finding a memory leak in my Mono for Android application. I believe I am following all best practices, outlined later, but I keep getting OutOfMemoryError after a consistent, reproducible number of run-throughs of an activity.

使用 DDMS 在模拟器上我可以看到我的应用程序消耗约200额外的数据对象约每一次的内存30KB我们的 ViewFlipper 翻到下一页。我们还消耗等资源,但在低得多的速率

Using ddms on the emulator I can see that my app is consuming around 200 extra "data objects" and about 30kB of memory every time our ViewFlipper flips to the next page. We are also consuming other resources, but at a much lower rate.

我用 ViewFlipper 有点别出心裁;它只是翻转在一个方向,并删除已显示的查看取值:

I use the ViewFlipper a little unconventionally; it only flips in one direction, and remove the Views that have already been shown:

while (flipper.ChildCount > 2)
{
    flipper.RemoveViewAt(0);
}

我已经采取非常谨慎,以的Dispose()我们使用任何查看取值任何引用,因为在这个博客帖子描述。我使用使用宗教为所有UI组件(自动的Dispose()的范围末尾的对象):

I have taken great care to Dispose() of any references to any Views we have used, as described in this blog post. I use using religiously for all UI components (which automatically Dispose() the object at the end of the scope):

using (TextView questionView = header.FindViewById<TextView>(Resource.Id.question))
{
    questionView.Text = question.Text;
}

这似乎并没有对内存泄漏任何影响。我用的是同样的模式,每当我打开位图 S(通常是PNG文件,小于20KB大小),这是我做的相当频繁。

This does not seem to have any effect on the memory leak. I use the same pattern whenever I load Bitmaps (usually PNG files, less than 20kB in size), which I do quite frequently.

更新的:我使用加载扩展方法的位图:

Update: I load bitmaps using an extension method:

public static Bitmap BitmapFromAsset(this Context context, String asset)
{
    Bitmap bitmap;
    using (Stream stream = context.Assets.Open(asset))
    {
        bitmap = BitmapFactory.DecodeStream(stream);
        stream.Close();
    }
    return bitmap;
}

位图,然后用这样的:

The bitmaps are then used like this:

using (Bitmap b = this.BitmapFromAsset(path))
{
    imageView.SetImageBitmap(b);
}

更新的:作为阿兰达以下建议,我用的代表,所以这是我的code一个共同的模式:

Update: As Aranda suggests below, I use delegates, so this is a common pattern in my code:

using (View button = FindViewById(Resource.Id.button))
{
    button.Click += delegate
    {
        // do something
    };
}

更改,这样我删除处理时,查看被移除使得泄漏没有什么区别。

Changing this so that I remove the handlers when the View is removed makes no difference in the leaking.

更新的:错误贴有Xamarin与示例项目

推荐答案

我有一个略微相似,而且很难找到问题(albiet WP7上,但它仍然相关,因为它是所有.NET)。原来我从另一个类,它没有超出范围附加了一些代表对我GameScreen类。确保你正在做的 - 。=连接的任何事件和代表以及失去了参照视图实例

I had a slightly similar, and very hard to find issue (albiet on WP7, but it's still relevant as it's all .Net). Turned out I had attached some delegates to my GameScreen class from another class that didn't go out of scope. Make sure you are doing -= any attached events and delegates as well as losing the reference to the view instance.

这篇关于单声道为Android - 的OutOfMemoryError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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