Bug 28606 - App crashes with Java.Lang.OutOfMemoryError after taking a few pictures with the CameraAppDemo
Summary: App crashes with Java.Lang.OutOfMemoryError after taking a few pictures with ...
Alias: None
Product: Android
Classification: Xamarin
Component: Samples ()
Version: 4.20.0
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: ---
Assignee: Peter Collins
Depends on:
Reported: 2015-03-31 16:38 UTC by Jon Goldberger [MSFT]
Modified: 2015-07-07 14:35 UTC (History)
4 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Jon Goldberger [MSFT] 2015-03-31 16:38:57 UTC
## Description

Taking a few pictures with the CameraAppDemo sample causes  the app to crash with a Java.Lang.OutOfMemoryError. I am not sure if this is just and issue with the sample or a deeper issue with Xamarin.Android or Android itself. If it is an issue with the sample because the sample is not releasing resources, then the sample should be updated to properly release resources to avoid the Java.Lang.OutOfMemoryError exception. 

## Steps to reproduce

1. Download and open the CameraAppDemo from http://developer.xamarin.com/recipes/android/other_ux/camera_intent/take_a_picture_and_save_using_camera_app/

2. Load and deploy the app to an Android device with a camera.

3. Click the "Open Camera" button and take a picture.

4. Click the "Save" button. This returns you to the initial page... note that the picture is not displayed in the ImageView due to an error in the code.. that the ImageView Bitmap is only set in OnCreate and OnCreate is not called when OnActivityResult is called. But this does not matter for the OutOfMemory issue, so you can ignore the fact that the picture is not shown.

5. Repeat steps 3 and 4 until you get a crash.It takes about 4 or 5 pictures on my Samsung Galaxy Tab 2 device, but will vary depending on the memory available on a particular device.

Expected result: App will not crash with an OutOfMemory exception.

Actual Result: App crashes with OutOfMemory exception with stack trace as follows:

>[MonoDroid] Java.Lang.OutOfMemoryError: Exception of type 'Java.Lang.OutOfMemoryError' was thrown.
>[MonoDroid] at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <IL 0x00011, 0x00068>
>[MonoDroid] at Android.Runtime.JNIEnv.CallStaticObjectMethod (intptr,intptr,Android.Runtime.JValue[]) [0x00064] in /Users/builder/data/lanes/1353/ac29b2c6/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:1161
>[MonoDroid] at Android.Graphics.BitmapFactory.DecodeFile (string,Android.Graphics.BitmapFactory/Options) [0x00034] in /Users/builder/data/lanes/1353/ac29b2c6/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.Graphics.BitmapFactory.cs:531
>[MonoDroid] at CameraAppDemo.BitmapHelpers.LoadAndResizeBitmap (string,int,int) [0x0005e] in /Users/apple/Downloads/CameraAppDemoNew/BitmapHelpers.cs:31
>[MonoDroid] at CameraAppDemo.MainActivity.OnActivityResult (int,Android.App.Result,Android.Content.Intent) [0x00058] in /Users/apple/Downloads/CameraAppDemoNew/MainActivity.cs:45
>[MonoDroid] at Android.App.Activity.n_OnActivityResult_IILandroid_content_Intent_ (intptr,intptr,int,int,intptr) [0x00014] in /Users/builder/data/lanes/1353/ac29b2c6/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.App.Activity.cs:2091
>[MonoDroid] at (wrapper dynamic-method) object.d128ad6c-d129-4f09-9aa4-4c311a53fd58 (intptr,intptr,int,int,intptr) <IL 0x00023, 0x0005b>
>[MonoDroid]   --- End of managed exception stack trace ---
>[MonoDroid] java.lang.OutOfMemoryError
>[MonoDroid] 	at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
>[MonoDroid] 	at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:623)
>[MonoDroid] 	at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:378)
>[MonoDroid] 	at cameraappdemo.MainActivity.n_onActivityResult(Native Method)
>[MonoDroid] 	at cameraappdemo.MainActivity.onActivityResult(MainActivity.java:29)
>[MonoDroid] 	at android.app.Activity.dispatchActivityResult(Activity.java:5472)
>[MonoDroid] 	at android.app.ActivityThread.deliverResults(ActivityThread.java:3402)
>[MonoDroid] 	at android.app.ActivityThread.handleSendResult(ActivityThread.java:3449)
>[MonoDroid] 	at android.app.ActivityThread.access$1200(ActivityThread.java:150)
>[MonoDroid] 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1328)
>[MonoDroid] 	at android.os.Handler.dispatchMessage(Handler.java:99)
>[MonoDroid] 	at android.os.Looper.loop(Looper.java:175)
>[MonoDroid] 	at android.app.ActivityThread.main(ActivityThread.java:5279)
>[MonoDroid] 	at java.lang.reflect.Method.invokeNative(Native Method)
>[MonoDroid] 	at java.lang.reflect.Method.invoke(Method.java:511)
>[MonoDroid] 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
>[MonoDroid] 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
>[MonoDroid] 	at dalvik.system.NativeStart.main(Native Method)
>[mono-rt] Stacktrace:
>[mono-rt]   at <unknown> <0xffffffff>
>[mono-rt]   at (wrapper managed-to-native) object.wrapper_native_0x40951cfd (intptr,intptr,intptr) <IL 0x00027, 0xffffffff>
>[mono-rt]   at Android.Runtime.JNIEnv.CallObjectMethod (intptr,intptr) [0x00040] in /Users/builder/data/lanes/1353/ac29b2c6/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:174
>[mono-rt]   at Java.Lang.Throwable.get_Message () [0x00043] in /Users/builder/data/lanes/1353/ac29b2c6/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Java.Lang.Throwable.cs:234
>[mono-rt]   at (wrapper runtime-invoke) <Module>.runtime_invoke_object__this__ (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff>
>[mono-rt]   at <unknown> <0xffffffff>
>[mono-rt]   at (wrapper dynamic-method) object.d128ad6c-d129-4f09-9aa4-4c311a53fd58 (intptr,intptr,int,int,intptr) <IL 0x00040, 0x000cc>
>[mono-rt]   at (wrapper native-to-managed) object.d128ad6c-d129-4f09-9aa4-4c311a53fd58 (intptr,intptr,int,int,intptr) <IL 0x00026, 0xffffffff>
>[mono-rt] =================================================================
>[mono-rt] Got a SIGSEGV while executing native code. This usually indicates
>[mono-rt] a fatal error in the mono runtime or one of the native libraries 
>[mono-rt] used by your application.
>[mono-rt] =================================================================
>[libc] Fatal signal 11 (SIGSEGV) at 0xdeadd00d (code=1), thread 27514 (o.CameraAppDemo)

## My Environment:

=== Xamarin Studio ===

Version 5.8.1 (build 8)
Installation UUID: 2dc9022f-f9a8-424f-8284-bf224cbbfde0
	Mono 3.12.1 ((detached/b63df7d)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 312010002

=== Apple Developer Tools ===

Xcode 6.2 (6776)
Build 6C131e

=== Xamarin.Mac ===

Version: (Business Edition)

=== Xamarin.Android ===

Version: (Business Edition)
Android SDK: /Users/apple/Library/Developer/Xamarin/android-sdk-mac_x86
	Supported Android versions:
		2.2    (API level 8)
		2.3    (API level 10)
		3.1    (API level 12)
		4.0    (API level 14)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
Java SDK: /usr
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-466.1-11M4716)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-466.1, mixed mode)

=== Xamarin Android Player ===

Version: Unknown version
Location: /Applications/Xamarin Android Player.app

=== Xamarin.iOS ===

Version: (Business Edition)
Hash: 7e1f69e
Build date: 2015-03-12 15:35:49-0400

=== Build Information ===

Release ID: 508010008
Git revision: f8e52bf493869b6a320a95400553dc9ea8b7ef22
Build date: 2015-03-13 16:16:49-04
Xamarin addins: 84d611cc17567a68daad719327e90af06d2ae327

=== Operating System ===

Mac OS X 10.10.2
Darwin Jons-iMac.local 14.1.0 Darwin Kernel Version 14.1.0
    Thu Feb 26 19:26:47 PST 2015
    root:xnu-2782.10.73~1/RELEASE_X86_64 x86_64
Comment 1 Jon Goldberger [MSFT] 2015-04-01 15:09:42 UTC
After speaking with Jon P, I added GC.Collect() to the end of OnActivityResult and that resolves the issue. Apparently in this case you do need to call GC.Collect() to dispose of the Java side Bitmap.

I am no longer seeing any memory leakage. I am going to leave this issue open, though, so that the sample can be updated.
Comment 3 Peter Collins 2015-06-09 12:14:43 UTC
I'm not able to reproduce this with the sample from the web (which now has the GC.Collect () call). I'm also unable to reproduce this error with the version of the sample found in the monodroid-samples repo which wraps bitmap usage in a 'Using' block.

@Asimk can you link me to the project you are using to reproduce this issue?
Comment 5 asimk 2015-06-09 12:46:49 UTC
I have provided the sample in above comment, so marking this issue as confirmed.
Comment 6 Peter Collins 2015-06-09 13:19:10 UTC
@Asimk the version of the sample you have provided has the GC.Collect () line commented out (MainActivity.cs L. 52). If you uncomment this line, as is provided by the current version of the recipe on the site, you should no longer be able to reproduce this.
Comment 7 asimk 2015-07-07 14:35:40 UTC
I have checked this issue with updated sample using latest build and observed that it is working fine, hence closing this issue.
I have checked this issue with XS and VS both using Nexus 5.

Environment Info:
XVS 3.11.666