Memory error in Android after capturing images - android

In my Android application I'm trying to capture three images and upload this three images. I'm capturing two images one by one no problem but when I capture third images the application crashes with error:
09-26 16:17:31.398: ERROR/AndroidRuntime(24115): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
09-26 16:17:31.398: ERROR/AndroidRuntime(24115): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
09-26 16:17:31.398: ERROR/AndroidRuntime(24115): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:459)
09-26 16:17:31.398: ERROR/AndroidRuntime(24115): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:271)
09-26 16:17:31.398: ERROR/AndroidRuntime(24115): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:296)
How can I solve the error like this?

Related

FATAL EXCEPTION: FrescoDecodeExecutor-2

I got the crash FATAL EXCEPTION: FrescoDecodeExecutor-2
Throwing OutOfMemoryError "Failed to allocate a 64256268 byte allocation with 16777216 free bytes and 49MB until OOM"
StackTrace :
java.lang.OutOfMemoryError: Failed to allocate a 64256268 byte allocation with 16777216 free bytes and 49MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:905)
at android.graphics.Bitmap.createBitmap(Bitmap.java:882)
at android.graphics.Bitmap.createBitmap(Bitmap.java:849)
at com.facebook.imagepipeline.memory.BitmapPool.alloc(BitmapPool.java:51)
at com.facebook.imagepipeline.memory.BitmapPool.alloc(BitmapPool.java:26)
at com.facebook.imagepipeline.memory.BasePool.get(BasePool.java:255)
at com.facebook.imagepipeline.platform.ArtDecoder.decodeStaticImageFromStream(ArtDecoder.java:147)
at com.facebook.imagepipeline.platform.ArtDecoder.decodeJPEGFromEncodedImage(ArtDecoder.java:127)
at com.facebook.imagepipeline.decoder.DefaultImageDecoder.decodeJpeg(DefaultImageDecoder.java:173)
at com.facebook.imagepipeline.decoder.DefaultImageDecoder$1.decode(DefaultImageDecoder.java:56)
at com.facebook.imagepipeline.decoder.DefaultImageDecoder.decode(DefaultImageDecoder.java:119)
at com.facebook.imagepipeline.producers.DecodeProducer$ProgressiveDecoder.doDecode(DecodeProducer.java:254)
at com.facebook.imagepipeline.producers.DecodeProducer$ProgressiveDecoder.access$300(DecodeProducer.java:113)
at com.facebook.imagepipeline.producers.DecodeProducer$ProgressiveDecoder$1.run(DecodeProducer.java:152)
at com.facebook.imagepipeline.producers.JobScheduler.doJob(JobScheduler.java:202)
at com.facebook.imagepipeline.producers.JobScheduler.access$000(JobScheduler.java:22)
at com.facebook.imagepipeline.producers.JobScheduler$1.run(JobScheduler.java:73)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at com.facebook.imagepipeline.core.PriorityThreadFactory$1.run(PriorityThreadFactory.java:51)
at java.lang.Thread.run(Thread.java:761)
Image is 5,34 Mo big 5344x3006 and was shoot by the phone Motorola XT1562, Android 7.1.1
implementation 'com.facebook.fresco:fresco:1.9.0'
Of course it does not reproduced every time the app is running
What's wrong ? Help ! :-)

Android "Shutting down VM" with no stacktrace

I have recently faced this issue a lot and I am not able to find any solution to it.
I am constantly getting Shutting down VM and logcat doesn't show any trace of this issue.
06-08 20:34:25.288 20416-20416/my.package.dev D/app_BaseRecyclerView﹕ onCreateViewHolder begins
06-08 20:34:25.324 20416-20416/my.package.dev D/AndroidRuntime﹕ Shutting down VM
06-08 20:34:30.969 20416-20425/my.package.dev I/art﹕ Thread[5,tid=20425,WaitingInMainSignalCatcherLoop,Thread*=0xb4a33800,peer=0x12c000a0,"Signal Catcher"]: reacting to signal 3
06-08 20:34:30.969 20416-20425/my.package.dev I/art﹕ [ 06-08 20:34:31.169 617: 639 I/Process ]
Sending signal. PID: 617 SIG: 3
Do you know how to make the stacktrace visible?
If you are using Google Analytics, try to disable ExceptionReporting if it is set to true

Android Hello World app crashes: IllegalArgumentException from Surface.lockCanvasNative

I am trying to run the simplest Hello World example app and looks like I can crash it even all it does is showing a TextView.
Steps to reproduce: Launch it in the emulator (1.5). Open the app - it shows the text view, then lock the phone by pressing "end call" button. Unlock by pressing "menu", then press "back". App crashes with "The process android.process.acore has stopped unexpectedly..." and Force Close button. It does not happen all the time, but I am able to reproduce it every time after wiping the user data at least.
I wonder if default Hello World is missing a detail which was considered too advanced for a hello world... Such problems with a basic example does not look very encouraging =)
Any idea what detail is missing and how to avoid this crash?
Stack (I've truncated timestamps):
37.478: DEBUG/KeyguardViewMediator(576): wakeWhenReadyLocked(82)
37.481: DEBUG/KeyguardViewMediator(576): handleWakeWhenReady(82)
37.481: DEBUG/KeyguardViewMediator(576): pokeWakelock(5000)
39.110: DEBUG/KeyguardViewMediator(576): pokeWakelock(5000)
39.140: WARN/InputManagerService(576): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy#436e3fc0
40.990: ERROR/IMemory(679): binder=0x238aa8 transaction failed fd=-2147483647, size=0, err=-2147483646 (Unknown error: 2147483646)
40.990: ERROR/IMemory(679): cannot dup fd=-2147483647, size=0, err=-2147483646 (Bad file number)
40.990: ERROR/IMemory(679): cannot map BpMemoryHeap (binder=0x238aa8), size=0, fd=-1 (Bad file number)
40.990: ERROR/Surface(679): Couldn't map Surface's heap (binder=0x238aa8, heap=0x238b00)
40.990: DEBUG/AndroidRuntime(679): Shutting down VM
40.990: WARN/dalvikvm(679): threadid=3: thread exiting with uncaught exception (group=0x4000fe70)
40.990: ERROR/AndroidRuntime(679): Uncaught handler: thread main exiting due to uncaught exception
40.990: ERROR/AndroidRuntime(679): java.lang.IllegalArgumentException
40.990: ERROR/AndroidRuntime(679): at android.view.Surface.lockCanvasNative(Native Method)
40.990: ERROR/AndroidRuntime(679): at android.view.Surface.lockCanvas(Surface.java:196)
40.990: ERROR/AndroidRuntime(679): at android.view.ViewRoot.draw(ViewRoot.java:1175)
40.990: ERROR/AndroidRuntime(679): at android.view.ViewRoot.performTraversals(ViewRoot.java:1030)
40.990: ERROR/AndroidRuntime(679): at android.view.ViewRoot.handleMessage(ViewRoot.java:1482)
40.990: ERROR/AndroidRuntime(679): at android.os.Handler.dispatchMessage(Handler.java:99)
40.990: ERROR/AndroidRuntime(679): at android.os.Looper.loop(Looper.java:123)
40.990: ERROR/AndroidRuntime(679): at android.app.ActivityThread.main(ActivityThread.java:3948)
40.990: ERROR/AndroidRuntime(679): at java.lang.reflect.Method.invokeNative(Native Method)
40.990: ERROR/AndroidRuntime(679): at java.lang.reflect.Method.invoke(Method.java:521)
40.990: ERROR/AndroidRuntime(679): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
40.990: ERROR/AndroidRuntime(679): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
40.990: ERROR/AndroidRuntime(679): at dalvik.system.NativeStart.main(Native Method)
41.000: INFO/Process(576): Sending signal. PID: 679 SIG: 3
41.000: INFO/dalvikvm(679): threadid=7: reacting to signal 3
41.030: ERROR/ActivityThread(576): Failed to find provider info for android.server.checkin
41.030: ERROR/Checkin(576): Error reporting crash: java.lang.IllegalArgumentException: Unknown URL content://android.server.checkin/crashes
41.070: INFO/dalvikvm(679): Wrote stack trace to '/data/anr/traces.txt'
50.940: WARN/ActivityManager(576): Launch timeout has expired, giving up wake lock!
50.980: WARN/ActivityManager(576): Activity idle timeout for HistoryRecord{4366ac40 {com.android.launcher/com.android.launcher.Launcher}}
sounds to me more like something wrong with your emulator. Why dont you delete your emulator and create a new one and try again with a fresh project.
Hope that will help you. BTW why are you using emulator on 1.5?? Start to build applications for min 2.1.
Good luck.

Better way of finding out where resources are going?

I have a memory leak in my app. So naturally I want to fix it. The issue is either I don't know how to use the DDMS and memory allocation tools or they are peices of crap (or both). So I'm wondering if there is another way I can figure out where all of my resources are being used or if someone can enlighten me on how to use the DDMS tools.
PS: Yes, I know I have to click the debug button in the DDMS and then cause a HPROF dump and/of use the Update heap button and do a GC. I can view the details of both but I can't see any of the objects I have created. In short I can't read what I'm viewing.
Any help or enlightenment is greatly appreciated.
~Aedon
Edit 1:
I added some logging throughout the drawing methods in my custom views. After some experimentation, I discoverd that the memory leak seams to be coming from this method.
/** Update the gauge independent static buffer cache for the background. */
private void regenerate() {
mNeedRegen = false;
// Prevent memory leaks by disposing of old bitmaps.
if (mBackground != null) { mBackground.recycle(); mBackground = null; }
// Our new drawing area
Log.d(TAG, getWidth() + "\t" + getHeight());
mBackground = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
Canvas backCanvas = new Canvas(mBackground);
float scale = (float)getWidth();
backCanvas.scale(scale, scale);
drawRim(backCanvas);
drawFace(backCanvas);
drawTitle(backCanvas);
if (!(this instanceof EmptySpace)) { drawGroupIcon(backCanvas); }
regenerateBackground(backCanvas);
}
Now after some fiddling around in my app, I managed to cause this error:
dalvikvm-heap E 195364-byte external allocation too large for this process.
2935 dalvikvm E Out of memory: Heap Size=4871KB, Allocated=2636KB, Bitmap Size=19528KB
2935 GraphicsJNI E VM won't let us allocate 195364 bytes
2935 AndroidRuntime D Shutting down VM
2935 dalvikvm W threadid=1: thread exiting with uncaught exception (group=0x400259f8)
2935 AndroidRuntime E FATAL EXCEPTION: main
2935 AndroidRuntime E java.lang.OutOfMemoryError: bitmap size exceeds VM budget
2935 AndroidRuntime E at android.graphics.Bitmap.nativeCreate(Native Method)
2935 AndroidRuntime E at android.graphics.Bitmap.createBitmap(Bitmap.java:574)
2935 AndroidRuntime E at com.android.appion.arm.widgets.GaugeBase.regenerate(GaugeBase.java:239)
2935 AndroidRuntime E at com.android.appion.arm.widgets.GaugeBase.onSizeChanged(GaugeBase.java:86)
2935 AndroidRuntime E at android.view.View.setFrame(View.java:7101)
2935 AndroidRuntime E at android.view.View.layout(View.java:7028)
2935 AndroidRuntime E at android.widget.GridView.setupChild(GridView.java:1316)
2935 AndroidRuntime E at android.widget.GridView.makeAndAddView(GridView.java:1222)
2935 AndroidRuntime E at android.widget.GridView.makeRow(GridView.java:265)
2935 AndroidRuntime E at android.widget.GridView.fillSpecific(GridView.java:463)
2935 AndroidRuntime E at android.widget.GridView.layoutChildren(GridView.java:1122)
2935 AndroidRuntime E at android.widget.AbsListView.onLayout(AbsListView.java:1147)
2935 AndroidRuntime E at android.view.View.layout(View.java:7034)
2935 AndroidRuntime E at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
2935 AndroidRuntime E at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
2935 AndroidRuntime E at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
2935 AndroidRuntime E at android.view.View.layout(View.java:7034)
2935 AndroidRuntime E at android.widget.SlidingDrawer.onLayout(SlidingDrawer.java:331)
2935 AndroidRuntime E at android.view.View.layout(View.java:7034)
2935 AndroidRuntime E at android.widget.RelativeLayout.onLayout(RelativeLayout.java:909)
2935 AndroidRuntime E at android.view.View.layout(View.java:7034)
2935 AndroidRuntime E at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
2935 AndroidRuntime E at android.view.View.layout(View.java:7034)
2935 AndroidRuntime E at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
2935 AndroidRuntime E at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
2935 AndroidRuntime E at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
2935 AndroidRuntime E at android.view.View.layout(View.java:7034)
2935 AndroidRuntime E at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
2935 AndroidRuntime E at android.view.View.layout(View.java:7034)
2935 AndroidRuntime E at android.view.ViewRoot.performTraversals(ViewRoot.java:1049)
2935 AndroidRuntime E at android.view.ViewRoot.handleMessage(ViewRoot.java:1744)
2935 AndroidRuntime E at android.os.Handler.dispatchMessage(Handler.java:99)
2935 AndroidRuntime E at android.os.Looper.loop(Looper.java:144)
2935 AndroidRuntime E at android.app.ActivityThread.main(ActivityThread.java:4937)
2935 AndroidRuntime E at java.lang.reflect.Method.invokeNative(Native Method)
2935 AndroidRuntime E at java.lang.reflect.Method.invoke(Method.java:521)
2935 AndroidRuntime E at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
2935 AndroidRuntime E at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
2935 AndroidRuntime E at dalvik.system.NativeStart.main(Native Method)
The error itself makes sense. I ran out of memory. What doesn't make sense is the bitmap I'm trying to make is 221px by 221px but aparently is 19528kb. If my math is right 221 * 221 = 48841 * 4 = 195364 bytes = 190.7kb. This doesn't make any sense at all. Anywho, please take a look and see what you can find. BTW, the line in question from the error is the following (from the regenerate method)
mBackground = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
First, I think the Dalvik message is showing the wrong units whereas the GraphicsJNI error is correct:
dalvikvm E Out of memory: Heap
Size=4871KB, Allocated=2636KB, Bitmap
Size=19528KB 2935
GraphicsJNI E VM won't let us
allocate 195364 bytes
Keep in mind you can catch the memory error:
try{
mBackground = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
} catch(OutOfMemoryError e){
//do something
}
If your bitmaps are always the same size, I would recommend re-using them. If you are creating and destroying bitmaps fairly quickly, you run the risk of out-pacing the Garbage Collector and getting into this state.
--Edit--
As for your original question. I typically do the following when looking for a memory leak.
Initialization Steps
Make sure your app is set to be debuggable in the manifest
Launch your app, open DDMS and select your application.
Select the Allocation Tracker view and Start Tracking
Analysis
Let your app run, or perform some specific activities in your app that are causing memory issues.
Click "Get Allocations" to update the tracker. You can now stop tracking if you like.
In the filter, type your application name to narrow down the results.
You need to think about what you are seeing here and whether it makes sense for your application. Should there be 1000's of small memory allocations? Most likely not. At this point you can click on an allocation and see exactly which method and line number is causing the allocation.
Another route to take is to enable heap updates for your app and use the heap explorer to track allocations. The heap explorer does not tell you where the allocation is coming from, but what it will tell you is that for instance:
The number of 1k allocations is
growing and never shrinking.
You can then go into the Allocation Tracker and sort allocations by size to see where you are performing 1k allocations.
I like to use the allocation tracker tool.
http://android-developers.blogspot.com/2009/02/track-memory-allocations.html
I realize this isn't what you were asking about...
I've found the following documentation on Android memory leaks to be worth reading.
http://developer.android.com/resources/articles/avoiding-memory-leaks.html
...as well as the other posts on optimization at http://android-developers.blogspot.com/search/label/Optimization

Out of memory error when using Google Maps API

I have an application here that has a feature of showing a POI on the map. It's only one POI and it is only drawn when it's actually within the visible screen area. It works perfectly for a while but if I play around zooming in and out and dragging, it will eventually crash. According to Logcat, the reason is always an OutOfMemory error.
At first, I thought it was a bug on the Google Maps API. After some researching and seeing some Romain Guy posts, I was kind of convinced that I had done something stupid on my app that kept me running out of memory from time to time.
Then I ran some more detailed tests with Heap Analyzer (Eclipse) on and I saw that even though I had 2+, sometimes 3+ megabytes left of free memory, I was still getting those annoying Force Close messages caused by OutOfMemoryError. Most of the times it crashes when trying to allocate some 614kb chunk of memory regardless of how much I have left.
This problem happens a lot on Nexus One 2.2.1 and on HTC Evo 2.1. After some little testing, I got no crash neither on G1 1.6 nor Samsung Galaxy S i9000 2.1. But I cannot say for sure the G1 and Galaxy will not show this problem after more testing.
I can only think of a memory fragmentation problem. I hope this problem has a solution. I will also be glad if I can just catch this error and prevent the app from crashing.
If it helps, here's the logcat:
09-29 08:58:06.661: ERROR/dalvikvm-heap(1552): 648000-byte external allocation too large for this process.
09-29 08:58:06.661: ERROR/dalvikvm(1552): Out of memory: Heap Size=9991KB, Allocated=6980KB, Bitmap Size=14510KB
09-29 08:58:06.661: ERROR/(1552): VM won't let us allocate 648000 bytes
09-29 08:58:06.672: DEBUG/AndroidRuntime(1552): Shutting down VM
09-29 08:58:06.672: WARN/dalvikvm(1552): threadid=3: thread exiting with uncaught exception (group=0x4001b390)
09-29 08:58:06.672: ERROR/AndroidRuntime(1552): Uncaught handler: thread main exiting due to uncaught exception
09-29 08:58:06.681: ERROR/AndroidRuntime(1552): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
09-29 08:58:06.681: ERROR/AndroidRuntime(1552): at android.graphics.Bitmap.nativeCreate(Native Method)
09-29 08:58:06.681: ERROR/AndroidRuntime(1552): at android.graphics.Bitmap.createBitmap(Bitmap.java:569)
09-29 08:58:06.681: ERROR/AndroidRuntime(1552): at com.google.android.maps.ZoomHelper.createSnapshot(ZoomHelper.java:422)
09-29 08:58:06.681: ERROR/AndroidRuntime(1552): at com.google.android.maps.ZoomHelper.beginZoom(ZoomHelper.java:186)
09-29 08:58:06.681: ERROR/AndroidRuntime(1552): at com.google.android.maps.MapView$2.onScaleBegin(MapView.java:371)
09-29 08:58:06.681: ERROR/AndroidRuntime(1552): at android.view.ScaleGestureDetector.onTouchEvent(ScaleGestureDetector.java:208)
09-29 08:58:06.681: ERROR/AndroidRuntime(1552): at com.google.android.maps.MapView.onTouchEvent(MapView.java:646)
09-29 08:58:06.681: ERROR/AndroidRuntime(1552): at android.view.View.dispatchTouchEvent(View.java:3709)
09-29 08:58:06.681: ERROR/AndroidRuntime(1552): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:874)
09-29 08:58:06.681: ERROR/AndroidRuntime(1552): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
09-29 08:58:06.681: ERROR/AndroidRuntime(1552): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
09-29 08:58:06.681: ERROR/AndroidRuntime(1552): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
09-29 08:58:06.681: ERROR/AndroidRuntime(1552): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1701)
09-29 08:58:06.681: ERROR/AndroidRuntime(1552): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1116)
09-29 08:58:06.681: ERROR/AndroidRuntime(1552): at android.app.Activity.dispatchTouchEvent(Activity.java:2068)
09-29 08:58:06.681: ERROR/AndroidRuntime(1552): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1685)
09-29 08:58:06.681: ERROR/AndroidRuntime(1552): at android.view.ViewRoot.handleMessage(ViewRoot.java:1708)
09-29 08:58:06.681: ERROR/AndroidRuntime(1552): at android.os.Handler.dispatchMessage(Handler.java:99)
09-29 08:58:06.681: ERROR/AndroidRuntime(1552): at android.os.Looper.loop(Looper.java:123)
09-29 08:58:06.681: ERROR/AndroidRuntime(1552): at android.app.ActivityThread.main(ActivityThread.java:4595)
09-29 08:58:06.681: ERROR/AndroidRuntime(1552): at java.lang.reflect.Method.invokeNative(Native Method)
09-29 08:58:06.681: ERROR/AndroidRuntime(1552): at java.lang.reflect.Method.invoke(Method.java:521)
09-29 08:58:06.681: ERROR/AndroidRuntime(1552): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
09-29 08:58:06.681: ERROR/AndroidRuntime(1552): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
09-29 08:58:06.681: ERROR/AndroidRuntime(1552): at dalvik.system.NativeStart.main(Native Method)
I have been running into this problem on and off for the past month. The way I've worked around the issue in my code is by forcing the GC to clean up unreferenced bitmaps that I have wrapped in WeakReferences. This approach might not work for you since the crash that you pasted above seems to originate in the MapView. Regardless, try to sprinkle some System.gc() calls in key locations in your code and watch the system Log to see if the GC_EXPLICIT messages indicate a lot of objects/bytes freed. In the code I'm working on I've had to add a System.gc() at the end of my Adapter.getView() method to ensure that unused bitmaps are cleaned up by the next time getView() is called. This approach seems to have drastically reduced my run in with the
dreaded java.lang.OutOfMemoryError: bitmap size exceeds VM budget crash.
I have been having this EXACT same problem, I salted the code with some System.GC calls and it seemed to help a little bit, but this still happens.
I am also getting another problem which might be related where the map seems to flip out and go all random when you pinch zoom. This tends to happen sometimes before a crash.
This is all only happening only on an HTC Desire which I believe is the same phone as the Nexus One under the hood. (and similar in many ways to the EVO which also suffers from it).
Note: It is working 100% fine on our Arc-S, Galaxy V1, AVD emulator and Asus tablet.
I had the same problem , add the following line in manifest file under application tag
android:largeHeap = true . This worked for me

Categories

Resources