7

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)
DonGru
  • 13,532
  • 8
  • 45
  • 55
Rafael Ramos
  • 215
  • 1
  • 3
  • 9
  • I got the same error reports from my users who have OS version 1.5, 2.1 and 2.2. Stacktrace does not pass through my code so I'm unable to catch this error - application finishes with FC. – tomash Sep 29 '10 at 12:26
  • It really looks like a fragmentation problem. It's like my app has some memory leak that spreads tiny chunks of data all over my heap memory area. I would guess that this kind of behavior could be improved in the next versions of Dalvik because it may not be a bug but for sure it's not a 'good behavior' since you actually have memory but system is unable to use because of its fragmentation. Well, I'll keep trying to see what can be done about it. – Rafael Ramos Sep 29 '10 at 15:20
  • I know it's been over a year now since this question was asked but has a solution being developed for this issue? I'm curiuos. Thanks in advance! – Ahmed Faisal Oct 18 '11 at 16:29

3 Answers3

1

I had the same problem , add the following line in manifest file under application tag

android:largeHeap = true . This worked for me

Vignesh raja
  • 194
  • 1
  • 6
1

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.

Akos Cz
  • 12,711
  • 1
  • 37
  • 32
  • calling System.gc() should not be helping OOME. the VM isn't going to through OOME before first trying to run the GC itself. also, System.gc() does nothing for fragmentation. – Jeffrey Blattman Dec 18 '10 at 16:03
  • These OOME errors tend to happens more frequently on phones of the Galaxy family like: Nexus One and Nexus S. I didn't use the Galaxy Nexus that much to check if this issue also happens more on this phone compared to the others. Throw GC calls to clean up the memory together with careful coding is, as far as I know, the best way to avoid this problem. One can also catch the exception (as a Throwable) and at least prevent the app from force closing. But unfortunately, I'm not sure if there is a definitive solution for this issue... – Rafael Ramos Mar 05 '12 at 10:34
1

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.

Dan Harvey
  • 798
  • 1
  • 10
  • 23