开发者

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.ViewGrou开发者_运维知识库p.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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜