java.lang.OutOfMemoryError:Xamarin Android

时间:2016-05-23 12:34:07

标签: xamarin bitmap xamarin.android out-of-memory sigsegv

获取 java.lang.OutOfMemoryError 尝试获取位图的mutable副本的像素。

以下是代码:

public async Task<Bitmap> LoadScaledDownBitmapForDisplayAsync(string path, BitmapFactory.Options options, int reqWidth, int reqHeight)
{
    // Calculate inSampleSize
    options.InSampleSize = CalculateInSampleSize(options, reqWidth, reqHeight);
    // Decode bitmap with inSampleSize set       
    options.InJustDecodeBounds = true;        
    Bitmap mutableBitmap = BitmapFactory.DecodeFile(path, options);
    Bitmap bitmap = mutableBitmap.Copy(Bitmap.Config.Argb8888, true);        
    allpixels = new int[bitmap.Height * bitmap.Width];
    bitmap.GetPixels(allpixels, 0, bitmap.Width, 0, 0, bitmap.Width, bitmap.Height);
    Bitmap bitmapToDisplay = null;
    for (int i = 0; i < allpixels.Length; i++)
     {
     if (allpixels[i] == Color.Black)
     {
      allpixels[i] = Color.Gray;
      int alpha = 0x00;
      allpixels[i] = Color.Red;

      bitmap.SetPixels(allpixels, 0, bitmap.Width, 0, 0, bitmap.Width, bitmap.Height);
             }
            }

    return bitmap;
}

错误:

05-25 17:24:41.945 D/dalvikvm(12002): GC_FOR_ALLOC freed 701K, 2% free 259110K/261936K, paused 53ms, total 54ms
05-25 17:24:42.850 I/dalvikvm-heap(12002): Clamp target GC heap from 256.376MB to 256.000MB
05-25 17:24:42.850 D/dalvikvm(12002): GC_FOR_ALLOC freed 276K, 1% free 259383K/261936K, paused 59ms, total 59ms
05-25 17:24:42.890 I/dalvikvm-heap(12002): Clamp target GC heap from 256.643MB to 256.000MB
05-25 17:24:42.890 D/dalvikvm(12002): GC_FOR_ALLOC freed <1K, 1% free 259655K/261936K, paused 37ms, total 37ms
05-25 17:24:42.890 I/dalvikvm-heap(12002): Forcing collection of SoftReferences for 281824-byte allocation
05-25 17:24:42.930 I/dalvikvm-heap(12002): Clamp target GC heap from 258.635MB to 256.000MB
05-25 17:24:42.930 D/dalvikvm(12002): GC_BEFORE_OOM freed 10K, 1% free 259645K/261936K, paused 42ms, total 42ms
05-25 17:24:42.930 E/dalvikvm-heap(12002): Out of memory on a 281824-byte allocation.
05-25 17:24:42.930 I/dalvikvm(12002): "main" prio=5 tid=1 RUNNABLE
05-25 17:24:42.930 I/dalvikvm(12002):   | group="main" sCount=0 dsCount=0 obj=0x41caa578 self=0x41c88a60
05-25 17:24:42.930 I/dalvikvm(12002):   | sysTid=12002 nice=-16 sched=0/0 cgrp=apps handle=1074249724
05-25 17:24:42.930 I/dalvikvm(12002):   | state=R schedstat=( 763705373545 16690026208 56421 ) utm=75810 stm=560 core=3
05-25 17:24:42.930 I/dalvikvm(12002):   at mono.android.view.View_OnClickListenerImplementor.n_onClick(Native Method)
05-25 17:24:42.930 I/dalvikvm(12002):   at mono.android.view.View_OnClickListenerImplementor.onClick(View_OnClickListenerImplementor.java:29)
05-25 17:24:42.930 I/dalvikvm(12002):   at android.view.View.performClick(View.java:4475)
05-25 17:24:42.930 I/dalvikvm(12002):   at android.view.View$PerformClick.run(View.java:18786)
05-25 17:24:42.930 I/dalvikvm(12002):   at android.os.Handler.handleCallback(Handler.java:730)
05-25 17:24:42.930 I/dalvikvm(12002):   at android.os.Handler.dispatchMessage(Handler.java:92)
05-25 17:24:42.930 I/dalvikvm(12002):   at android.os.Looper.loop(Looper.java:176)
05-25 17:24:42.930 I/dalvikvm(12002):   at android.app.ActivityThread.main(ActivityThread.java:5419)
05-25 17:24:42.930 I/dalvikvm(12002):   at java.lang.reflect.Method.invokeNative(Native Method)
05-25 17:24:42.930 I/dalvikvm(12002):   at java.lang.reflect.Method.invoke(Method.java:525)
05-25 17:24:42.930 I/dalvikvm(12002):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
05-25 17:24:42.930 I/dalvikvm(12002):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
05-25 17:24:42.935 I/dalvikvm(12002):   at dalvik.system.NativeStart.main(Native Method)
05-25 17:24:42.935 I/dalvikvm(12002): 
05-25 17:24:42.935 W/dalvikvm(12002): JNI WARNING: JNI method called with exception pending
05-25 17:24:42.935 W/dalvikvm(12002):              in Lmono/android/view/View_OnClickListenerImplementor;.n_onClick:(Landroid/view/View;)V (SetIntArrayRegion)
05-25 17:24:42.935 W/dalvikvm(12002): Pending exception is:
05-25 17:24:42.935 I/dalvikvm(12002): java.lang.OutOfMemoryError:
05-25 17:24:42.935 I/dalvikvm(12002):   at mono.android.view.View_OnClickListenerImplementor.n_onClick(Native Method)
05-25 17:24:42.935 I/dalvikvm(12002):   at mono.android.view.View_OnClickListenerImplementor.onClick(View_OnClickListenerImplementor.java:29)
05-25 17:24:42.935 I/dalvikvm(12002):   at android.view.View.performClick(View.java:4475)
05-25 17:24:42.935 I/dalvikvm(12002):   at android.view.View$PerformClick.run(View.java:18786)
05-25 17:24:42.935 I/dalvikvm(12002):   at android.os.Handler.handleCallback(Handler.java:730)
05-25 17:24:42.935 I/dalvikvm(12002):   at android.os.Handler.dispatchMessage(Handler.java:92)
05-25 17:24:42.935 I/dalvikvm(12002):   at android.os.Looper.loop(Looper.java:176)
05-25 17:24:42.935 I/dalvikvm(12002):   at android.app.ActivityThread.main(ActivityThread.java:5419)
05-25 17:24:42.935 I/dalvikvm(12002):   at java.lang.reflect.Method.invokeNative(Native Method)
05-25 17:24:42.935 I/dalvikvm(12002):   at java.lang.reflect.Method.invoke(Method.java:525)
05-25 17:24:42.935 I/dalvikvm(12002):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
05-25 17:24:42.935 I/dalvikvm(12002):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
05-25 17:24:42.935 I/dalvikvm(12002):   at dalvik.system.NativeStart.main(Native Method)
05-25 17:24:42.935 I/dalvikvm(12002): "main" prio=5 tid=1 NATIVE
05-25 17:24:42.935 I/dalvikvm(12002):   | group="main" sCount=0 dsCount=0 obj=0x41caa578 self=0x41c88a60
05-25 17:24:42.935 I/dalvikvm(12002):   | sysTid=12002 nice=-16 sched=0/0 cgrp=apps handle=1074249724
05-25 17:24:42.935 I/dalvikvm(12002):   | state=R schedstat=( 763706606629 16692302499 56458 ) utm=75810 stm=560 core=3
05-25 17:24:42.935 I/dalvikvm(12002):   #00  pc 000012fe  /system/lib/libcorkscrew.so (unwind_backtrace_thread+29)
05-25 17:24:42.935 I/dalvikvm(12002):   #01  pc 00060b82  /system/lib/libdvm.so (dvmDumpNativeStack(DebugOutputTarget const*, int)+33)
05-25 17:24:42.935 I/dalvikvm(12002):   #02  pc 00054c20  /system/lib/libdvm.so (dvmDumpThreadEx(DebugOutputTarget const*, Thread*, bool)+395)
05-25 17:24:42.935 I/dalvikvm(12002):   #03  pc 00054c8e  /system/lib/libdvm.so (dvmDumpThread(Thread*, bool)+25)
05-25 17:24:42.935 I/dalvikvm(12002):   #04  pc 00038f00  /system/lib/libdvm.so
05-25 17:24:42.935 I/dalvikvm(12002):   #05  pc 0003af50  /system/lib/libdvm.so
05-25 17:24:42.935 I/dalvikvm(12002):   at mono.android.view.View_OnClickListenerImplementor.n_onClick(Native Method)
05-25 17:24:42.935 I/dalvikvm(12002):   at mono.android.view.View_OnClickListenerImplementor.onClick(View_OnClickListenerImplementor.java:29)
05-25 17:24:42.935 I/dalvikvm(12002):   at android.view.View.performClick(View.java:4475)
05-25 17:24:42.935 I/dalvikvm(12002):   at android.view.View$PerformClick.run(View.java:18786)
05-25 17:24:42.935 I/dalvikvm(12002):   at android.os.Handler.handleCallback(Handler.java:730)
05-25 17:24:42.935 I/dalvikvm(12002):   at android.os.Handler.dispatchMessage(Handler.java:92)
05-25 17:24:42.935 I/dalvikvm(12002):   at android.os.Looper.loop(Looper.java:176)
05-25 17:24:42.935 I/dalvikvm(12002):   at android.app.ActivityThread.main(ActivityThread.java:5419)
05-25 17:24:42.935 I/dalvikvm(12002):   at java.lang.reflect.Method.invokeNative(Native Method)
05-25 17:24:42.935 I/dalvikvm(12002):   at java.lang.reflect.Method.invoke(Method.java:525)
05-25 17:24:42.935 I/dalvikvm(12002):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
05-25 17:24:42.935 I/dalvikvm(12002):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
In mgmain JNI_OnLoad
05-25 17:24:42.940 I/dalvikvm(12002):   at dalvik.system.NativeStart.main(Native Method)
05-25 17:24:42.940 I/dalvikvm(12002): 
05-25 17:24:42.940 E/dalvikvm(12002): VM aborting
05-25 17:24:42.945 E/mono-rt (12002): Stacktrace:
05-25 17:24:42.945 E/mono-rt (12002): 

最后得到这条消息:

Got a SIGSEGV while executing native code. This usually indicates
05-25 17:24:42.985 E/mono-rt (12002): a fatal error in the mono runtime or one of the native libraries 
05-25 17:24:42.985 E/mono-rt (12002): used by your application.

任何想法或帮助都会非常有用。

1 个答案:

答案 0 :(得分:1)

我强烈建议您在位图代码周围使用using语句。这应该有助于处理正确的对等包装器。如果不使用using语句,请确保调用.Dipose()或将对等包装器设置为null以断开连接。

其次,我还建议通过GCGC.Collect()进行手动JavaSystem.GC()调用,以帮助减少操作结束时的工作集。

即:

using (var bitmap = HowIGetThisBitMap())
{
    //How I use this bitmap while in scope
}

GC.Collect(); //I then ensure I manually invoke the GC

是的,这与传统的.NET项目有些不同,但我们在移动设备上进行内存敏感操作。对于没有大RAM缓存的低端设备尤其如此。这是我们想要调用GC.Collect()的一个代码点。