Xamarin的Andr​​oid内存消耗增长无限的使用达到一定的阈值后, [英] Xamarin android memory consumption grows infinitely after usage hits a certain threshold

查看:664
本文介绍了Xamarin的Andr​​oid内存消耗增长无限的使用达到一定的阈值后,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Xamarin的Andr​​oid应用程序,它好像当应用程序的内存使用量达到一定的阈值140MB到160MB的应用程序将很快开始占用更多的内存,就好像在一个无限循环。我可以在垃圾收集器反复不停地尝试释放内存调试器输出中看到,但它似乎并没有工作。内存使用量将增长似乎没有边界。我看着它上升到超过了500MB内存之前,我决定杀应用程序。我从来没有得到一个内存不足的例外,这是很奇怪的。从我可以告诉有没有具体的一条code,这发生在我可以在不同的屏幕,并会发生同样的事情。我曾在多个设备上测试过,所以我知道它不只是一个问题,我的设备。我试图表现出一定的code,但我没有任何的罪魁祸首。

其中在我的应用程序中使用的组件,可能也许会导致一个问题是ReshSharp客户端,达网络Web客户,使用位图,与3个标签一个TabHost,位置管理器,并通过相机拍摄的照片。我难倒就这一个任何帮助是极大的AP preciated。

编辑: 这是可能的,我已经收窄,其中的问题之一可能是。我有一个tabhost用于拍照的内部摄像头的活动,我有一个在照片拍摄方法,该方法被调用抢购照片后,采取一些照片出现问题后。下面是该方法

 公共无效OnPictureTaken(byte []的数据,全球:: Android.Hardware.Camera C)
    {
        所以GC.Collect();
        位图B = BitmapExtensions.De codeBitmapFromArray(数据,宽,高);

        字模=新的Matrix();
        matrix.SetRotate(RotationDegrees,宽度/ 2F,高度/ 2F);

        变种bitmapScalled = Bitmap.CreateBitmap(二,0,0,宽度,高度,矩阵,真);

        变种D =全球:: Android.OS.Environment.ExternalStorageDirectory.Path +/ MyApp的/;
        如果(!Directory.Exists(d))的
            Directory.CreateDirectory(四);

        文件= D + Guid.NewGuid()的ToString()+.JPG。

        System.IO.StreamWriter SW =新System.IO.StreamWriter(文件);
        bitmapScalled.Com preSS(Bitmap.Com pressFormat.Jpeg,70,sw.BaseStream);
        sw.Close();

        全球:: Android.Locations.Location位置= CameraLocationManager.GetLastKnownLocation(CameraLocationManager.GetBestProvider(新标准(){精度= Accuracy.Fine},真));

        意向意图=新的意图(此的typeof(EditPhotoActivity));
        intent.PutExtra(LastKnownLocation,JsonConvert.SerializeObject(LastKnownLocation));
        intent.PutExtra(文件名,文件);
        // StartActivity(意向);
        StartCamera(); //重新启动摄像机preVIEW
        b.Recycle();
        B =零;
        sw.Dispose();
        bitmapScalled.Dispose();
        bitmapScalled = NULL;
        // 清理
        所以GC.Collect();
    }
 

解决方案

虽然Xamarin探查仍是preVIEW它帮了我很多跟踪我们在我们的应用程序的内存问题。读你最后的评论,我可以证实Xamarin洞察(v1.10.1)有显著巨大的内存签名(甚至可能泄漏)。当我们从我们的code删除它! - 该应用程序是表现超高速

底线 - 使用分析器来识别内存问题,如果你有Xamarin见解应用于您的应用程序 - 我建议将其删除,直至另行通知。

I have an Xamarin android application and it seems as though when the memory usage of the app hits a certain threshold 140mb to 160mb the app will rapidly start taking up more memory as if in an infinite loop. I can see in the debugger output that the garbage collector keeps repeatedly trying to free memory but it doesn't seem to work. The memory usage will grow seemingly without bound. I watched it rise to well over 500mb of memory before I decide kill the app. I NEVER get an out of memory exception which is really weird. From what I can tell there is no specific piece of code that this happens on I can be in various screens and the same thing will occur. I have tested on multiple devices so I know its not just a problem with my device. I would attempt to show some code but I don't have any culprits.

Among the components used in my app that could maybe cause an issue are ReshSharp clients, .net Webclients, use of bitmaps, a TabHost with 3 tabs, location manager, and taking of photos through the camera. I'm stumped on this one any help is greatly appreciated.

EDIT: It's possible that I have narrowed down where one of the problems might be. I have a camera activity inside of a tabhost for taking photos, I have a on photo taken method that gets called after snapping a picture, after taking several photos the problem arises. Here is the method

    public void OnPictureTaken(byte[] data, global::Android.Hardware.Camera c)
    {
        GC.Collect();
        Bitmap b = BitmapExtensions.DecodeBitmapFromArray(data, WIDTH, HEIGHT);

        Matrix matrix = new Matrix();
        matrix.SetRotate(RotationDegrees, WIDTH / 2f, HEIGHT / 2f);

        var bitmapScalled = Bitmap.CreateBitmap(b, 0, 0, WIDTH, HEIGHT, matrix, true);

        var d = global::Android.OS.Environment.ExternalStorageDirectory.Path + "/MyApp/";
        if (!Directory.Exists(d))
            Directory.CreateDirectory(d);

        file = d + Guid.NewGuid().ToString() + ".jpg";

        System.IO.StreamWriter sw = new System.IO.StreamWriter(file);
        bitmapScalled.Compress(Bitmap.CompressFormat.Jpeg, 70, sw.BaseStream);
        sw.Close();

        global::Android.Locations.Location location = CameraLocationManager.GetLastKnownLocation(CameraLocationManager.GetBestProvider(new Criteria() { Accuracy = Accuracy.Fine }, true));

        Intent intent = new Intent(this, typeof(EditPhotoActivity));
        intent.PutExtra("LastKnownLocation", JsonConvert.SerializeObject(LastKnownLocation));
        intent.PutExtra("Filename", file);
        //StartActivity(intent);
        StartCamera(); // restart camera preview
        b.Recycle();
        b = null;
        sw.Dispose();
        bitmapScalled.Dispose();
        bitmapScalled = null;
        // clean up
        GC.Collect();
    }

解决方案

Although Xamarin profiler is still in preview it helped me a lot tracking down memory issues we had in our application. Reading your last comment i can confirm Xamarin Insights (v1.10.1) has a significant huge memory signature (and might even leak). After we removed it from our code - the app was behaving super fast!

Bottom line - use the profiler to identify memory issues and if you have Xamarin Insights used in your app - i suggest to remove it until further notice.

这篇关于Xamarin的Andr​​oid内存消耗增长无限的使用达到一定的阈值后,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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