Reputation: 2721
Getting java.lang.OutOfMemoryError
While trying to GetPixels of mutable
copy of the Bitmap.
Here is the code:
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;
}
Error:
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):
And Finally Getting this Message:
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.
Any Idea or help would be really helpful.
Upvotes: 1
Views: 834
Reputation: 13176
I would highly recommend using using
statements all around your bitmap code. That should help dispose of the proper peer wrapper. If you don't use using
statements, then ensure you are calling .Dipose()
or setting the peer wrapper to null
to sever the connection.
Secondly, I would also recommend a manual GC
invoke via GC.Collect()
or JavaSystem.GC()
to help reduce the working set at the end of your operation.
i.e.:
using (var bitmap = HowIGetThisBitMap())
{
//How I use this bitmap while in scope
}
GC.Collect(); //I then ensure I manually invoke the GC
Yes this is somewhat against traditional .NET items, but we are on a mobile device where we have memory sensitive operations. This is especially the case for lower-end devices that don't have a large RAM cache. This is specifically one spot of code that we would want to call GC.Collect()
.
Upvotes: 1