gpt4 book ai didi

android - Xamarin android 内存消耗在使用量达到一定阈值后无限增长

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:24:15 26 4
gpt4 key购买 nike

我有一个 Xamarin android 应用程序,似乎当应用程序的内存使用量达到某个阈值 140mb 到 160mb 时,应用程序将迅速开始占用更多内存,就像在无限循环中一样。我可以在调试器输出中看到垃圾收集器不断重复尝试释放内存,但它似乎不起作用。内存使用量似乎无限制地增长。在我决定终止应用程序之前,我看到它增加了超过 500mb 的内存。我从来没有遇到过内存不足的异常,这真的很奇怪。据我所知,没有特定的代码 fragment 会发生这种情况,我可以在不同的屏幕上出现同样的事情。我已经在多个设备上进行了测试,所以我知道这不仅仅是我设备的问题。我会尝试展示一些代码,但我没有任何罪魁祸首。

在我的应用程序中使用的可能会导致问题的组件包括 ReshSharp 客户端、.net Web 客户端、位图的使用、具有 3 个选项卡的 TabHost、位置管理器以及通过相机拍摄照片。我对这个感到难过,非常感谢任何帮助。

编辑:我可能已经缩小了其中一个问题的范围。我在 tabhost 中有一个相机 Activity 用于拍照,我有一个在拍照后调用的拍照方法,在拍了几张照片后出现了问题。这是方法

    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();
}

最佳答案

虽然 Xamarin 探查器仍处于预览阶段,但它帮助我追踪了我们在应用程序中遇到的内存问题。阅读您的最后评论,我可以确认 Xamarin Insights (v1.10.1) 具有显着的巨大内存签名(甚至可能泄漏)。在我们将它从我们的代码中删除后 - 该应用程序的运行速度非常快!

底线 - 使用探查器识别内存问题,如果您的应用程序中使用了 Xamarin Insights - 我建议将其删除,直至另行通知。

关于android - Xamarin android 内存消耗在使用量达到一定阈值后无限增长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27722601/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com