xamarin / SignaturePad

MIT License
246 stars 150 forks source link

Xamarian.Forms Out of memory exception #83

Open Leonscape opened 7 years ago

Leonscape commented 7 years ago

I'm having a problem with the signature pad on android under forms. After around 35 uses the app crashes with an OOM exception inside android. This was tested on a J3 phone with version 2.1.1 With version 1.5.1 this problem doesn't happen, and I can repeat the calling of the page as many times as I like. I've attached a demo solution that exhibits this problem. TestSignaturePad.zip

07-10 16:40:23.766 I/MonoDroid( 6196): UNHANDLED EXCEPTION: 07-10 16:40:23.886 I/MonoDroid( 6196): Java.Lang.OutOfMemoryError: Failed to allocate a 2851212 byte allocation with 632184 free bytes and 617KB until OOM 07-10 16:40:23.886 I/MonoDroid( 6196): at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <3fd174ff54b146228c505f23cf75ce71>:0 07-10 16:40:23.886 I/MonoDroid( 6196): at Java.Interop.JniEnvironment+StaticMethods.CallStaticObjectMethod (Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue args) [0x00069] in :0 07-10 16:40:23.886 I/MonoDroid( 6196): at Java.Interop.JniPeerMembers+JniStaticMethods.InvokeObjectMethod (System.String encodedMember, Java.Interop.JniArgumentValue parameters) [0x00018] in :0 07-10 16:40:23.886 I/MonoDroid( 6196): at Android.Graphics.Bitmap.CreateBitmap (System.Int32 width, System.Int32 height, Android.Graphics.Bitmap+Config config) [0x0005a] in :0 07-10 16:40:23.886 I/MonoDroid( 6196): at Xamarin.Controls.InkPresenter.CreateBufferImage () [0x00047] in <1c8da6cdedc6484799a7f1284def41ac>:0 07-10 16:40:23.886 I/MonoDroid( 6196): at Xamarin.Controls.InkPresenter.OnDraw (Android.Graphics.Canvas canvas) [0x00044] in <1c8da6cdedc6484799a7f1284def41ac>:0 07-10 16:40:23.886 I/MonoDroid( 6196): at Android.Views.View.n_OnDraw_Landroid_graphicsCanvas (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_canvas) [0x0000f] in :0 07-10 16:40:23.886 I/MonoDroid( 6196): at (wrapper dynamic-method) System.Object:05e98750-69cc-4ddd-bc25-2dbaafb6a9ad (intptr,intptr,intptr) 07-10 16:40:23.886 I/MonoDroid( 6196): --- End of managed Java.Lang.OutOfMemoryError stack trace ---

jBijsterboschNL commented 7 years ago

We are experiencing a memory leak as well in the Android SignaturePad. We're running Xamarin.Forms version 2.3.4.270 and Xamarin.Controls.SignaturePad.Forms version 2.2.0.

We have a ContentPage with a SignaturePad.Forms.SignaturePadView control on it. When navigating to this page back and forth everything works as expected. When navigating to this page and draw something, then navigate back and again navigate to this page (with the SignaturePadView) and draw something (repeating this a couple of time), we also get the same exception as described above. We're not even calling the GetImageStreamAsync() method yet!

Could you investigate this issue, as we are running this on a Production environment. Might be that some Canvas, Bitmaps or something related doesn't get disposed properly inside the SignaturePad? Calling the SignaturePadView Clear() method doesn't solve this issue either.

Note: this ONLY happens on Android!

devjinho commented 7 years ago

I`m having a same issue.

After disposing the GetImagetStreamAsync() mehotd, calling the Clear() method bumps up a memory bit, but, still it occurs OutOfMemoryError. (In my case, 15+ times uses)

jBijsterboschNL commented 6 years ago

@mattleibow any updates on this issue?

markolazic88 commented 6 years ago

Hey guys, I just wrote a detailed blog post on the subject. The workaround is to invoke explicit garbage collection after collecting the signature. I used @Leonscape's TestSignaturePad solution to demonstrate how GC.Collect() helps.

jBijsterboschNL commented 6 years ago

Hi @markolazic88, thank you for your great detailed blog about this subject. Unfortunately the example that you've provided doesn't work how you've described it in your blog. The heap size isn't decreasing when a call to GC.Collect() has been made. In the Android Device Monitor I also got this strange error: "Invalid object in managed heap". This error also occurs when I hit the "Cause GC" button inside the Android Device Monitor tool and the heap size also won't shrink.

markolazic88 commented 6 years ago

Hey @jBijsterboschNL, thanks for pointing this out. Are you trying the example from my GitHub repo? I made a small mistake at line 27 of SignaturePage.xaml.cs, because I checked in GC.Collect(0) call instead of GC.Collect(). I was testing GC.Collect(0), which should perform only Minor collection on the Mono heap, but it does not do the trick for SignaturePad. You can see in the blog post that I was referring to GC.Collect() call. Also make sure that the line is uncommented. It is commented out by default to demonstrate that the issue exists.

Android Device Monitor works fine for me. Try other device (I am working on VS Android Emulator), or maybe something is not properly installed on your machine.

Android "Cause GC" will not shrink heap size in this case, as Xamarin is still holding the reference to the bitmap image. When GC.Collect() is called, it will remove the reference and then it will call Android "Cause GC", which now is able to remove the big image.

jBijsterboschNL commented 6 years ago

Hi @markolazic88, thanks for your quick reply. This morning I did try several attempts and I'd also noticed that there was a call to GC.Collect(0) and thought I'd also tested it with the default call GC.Collect(). Apparently not, because now it is working as you've described. Thanks for your assistance!

mattleibow commented 6 years ago

I know this is an old issue, but is it still happening? I have been updating the project and trying to get all the fixes in for a brand new release.

It may have been an issue with Xamarin.Forms or it may be SignaturePad. Times have changed, and things have improved. Hopefully.

If you want to try out the latest bits from the CI system, you can get them here: https://jenkins.mono-project.com/job/Components-SignaturePad-Windows/lastSuccessfulBuild/Azure/

I hope to release a preview soon so that this can tested in the real world due to some major improvements.

VladislavAntonyuk commented 5 years ago

@mattleibow it is still reproduced

ehansenp commented 4 years ago

I found that error. When i check all code, there is error type value properti.

VladislavAntonyuk commented 4 years ago

@ehansenp please create PR: https://github.com/VladislavAntonyuk/SignaturePad