简体   繁体   中英

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;

I want to create volatile Bitmap. When I open my application 3rd time, I have exception: Java.Lang.OutOfMemoryError.

If I use static Bitmap instead of volatile Bitmap, application works correctly and I can open it many times.

Why I can't use volatile Bitmap?

  • You should set a larger Java Heap size . Go to you Android project options -> Advanced and set Java Heap size to 1G or 2G.

  • Set android:largeHeap to true in the Android application manifest

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().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM