Bitmap.CreateBitmap 异常 - 内存不足

Bitmap.CreateBitmap exception - Out Of Memory

protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);
        global::Xamarin.Forms.Forms.Init (this, bundle);
        LoadApplication (new App ());
        if (bitmapChart != null && !bitmapChart.IsRecycled)
        {
            bitmapChart.Recycle();
        }
        bitmapChart = Android.Graphics.Bitmap.CreateBitmap (2000, 800, Android.Graphics.Bitmap.Config.Argb8888);            
    }

public volatile Android.Graphics.Bitmap bitmapChart;

我想创建可变位图。 当我第三次打开我的应用程序时,出现异常:Java.Lang.OutOfMemoryError.

如果我使用静态位图而不是易变位图,应用程序可以正常工作,我可以多次打开它。

为什么我不能使用 volatile Bitmap?

  • 你应该设置一个更大的Java Heap size。转到 Android 项目选项 -> 高级并将 Java Heap size 设置为 1G 或 2G。

  • 在 Android 应用程序清单中将 android:largeHeap 设置为 true

android:largeHeap Whether your application's processes should be created with a large Dalvik heap. This applies to all processes created for the application. It only applies to the first application loaded into a process; if you're using a shared user ID to allow multiple applications to use a process, they all must use this option consistently or they will have unpredictable results. Most apps should not need this and should instead focus on reducing their overall memory usage for improved performance. Enabling this also does not guarantee a fixed increase in available memory, because some devices are constrained by their total available memory.

To query the available memory size at runtime, use the methods getMemoryClass() or getLargeMemoryClass().