地图碎片 Google 地图 API v2 内存不足错误/内存泄漏

5
我在过去几天里阅读了许多类似的帖子,但没有一个能够明确回答这个问题。当我使用混合地图进行激烈的地图平移和缩放约2分钟后,就会出现OOM问题。应用程序在崩溃后似乎仍然保留着内存,因为第一次失败后需要更少的时间才能达到OOM。我将代码缩小到只有一个具有MapFragment的Activity,仍然遇到相同的OOM问题。我使用DDMS和MAT来尝试隔离问题,并添加了android:largeHeap="true",但这只是推迟了OOM的发生。屏幕重新定位不是问题,因为我在清单文件中设置了android:screenOrientation="portrait"。任何帮助都将不胜感激。
2014-08-25更新:这个问题据说在2013年5月就已经解决了? 2014-09-06更新:仍然没有成功,唯一的好选择是使用Normal而不是Hybrid。没有图像,GC似乎可以跟上。
类似的帖子: 使用Google Maps API时出现内存不足错误 Android Google Maps API v2的内存泄漏 Android Google Maps API,OutOfMemoryError MainActivity:
public class MainActivity_blank extends Activity {

GoogleMap mMap;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main_activity_blank);
    setContentView(R.layout.map_fragment);

     setUpMapIfNeeded();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main_activity_blank, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
        mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            // The Map is verified. It is now safe to manipulate the map.
            //startUsingMap();
            mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        }
    }
}
}

日志记录:

08-26 17:15:29.302: I/dalvikvm-heap(19642): Forcing collection of SoftReferences for 67108880-byte allocation
08-26 17:15:29.372: D/dalvikvm(19642): GC_BEFORE_OOM freed 822K, 65% free 37523K/106080K, paused 78ms, total 78ms
08-26 17:15:30.582: W/dalvikvm(19642): chunk is too big to transmit (length=49579336, 48435 bytes)
08-26 17:15:31.532: E/dalvikvm-heap(19642): Out of memory on a 67108880-byte allocation.
08-26 17:15:31.532: I/dalvikvm(19642): "GLThread 14876" prio=5 tid=19 RUNNABLE
08-26 17:15:31.532: I/dalvikvm(19642):   | group="main" sCount=0 dsCount=0 obj=0x42c120d8 self=0x787e8438
08-26 17:15:31.532: I/dalvikvm(19642):   | sysTid=19729 nice=1 sched=0/0 cgrp=apps handle=2021558416
08-26 17:15:31.532: I/dalvikvm(19642):   | state=R schedstat=( 51120733635 13119596807 80537 ) utm=1904 stm=3208 core=0
08-26 17:15:31.532: I/dalvikvm(19642):   at android.graphics.Bitmap.nativeCreate(Native Method)
08-26 17:15:31.532: I/dalvikvm(19642):   at android.graphics.Bitmap.createBitmap(Bitmap.java:924)
08-26 17:15:31.532: I/dalvikvm(19642):   at android.graphics.Bitmap.createBitmap(Bitmap.java:901)
08-26 17:15:31.532: I/dalvikvm(19642):   at android.graphics.Bitmap.createBitmap(Bitmap.java:868)
08-26 17:15:31.532: I/dalvikvm(19642):   at opl.a((null):-1)
08-26 17:15:31.532: I/dalvikvm(19642):   at okg.a((null):-1)
08-26 17:15:31.532: I/dalvikvm(19642):   at opy.a((null):-1)
08-26 17:15:31.532: I/dalvikvm(19642):   at opx.a((null):-1)
08-26 17:15:31.532: I/dalvikvm(19642):   at ojx.a((null):-1)
08-26 17:15:31.532: I/dalvikvm(19642):   at ojx.b((null):-1)
08-26 17:15:31.532: I/dalvikvm(19642):   at otq.k((null):-1)
08-26 17:15:31.532: I/dalvikvm(19642):   at otq.run((null):-1)
08-26 17:15:31.542: W/System.err(19642): OutOfMemory
08-26 17:15:31.642: D/dalvikvm(19642): GC_EXPLICIT freed 4803K, 64% free 38238K/106080K, paused 6ms+11ms, total 102ms
08-26 17:15:32.822: W/dalvikvm(19642): chunk is too big to transmit (length=49444016, 48303 bytes)
08-26 17:15:33.682: D/dalvikvm(19642): GC_FOR_ALLOC freed 3728K, 66% free 36916K/106080K, paused 65ms, total 65ms
08-26 17:15:34.752: W/dalvikvm(19642): chunk is too big to transmit (length=49398928, 48259 bytes)
08-26 17:15:35.462: I/dalvikvm-heap(19642): Forcing collection of SoftReferences for 67108880-byte allocation
08-26 17:15:35.512: D/dalvikvm(19642): GC_BEFORE_OOM freed 413K, 66% free 36502K/106080K, paused 52ms, total 52ms
08-26 17:15:36.742: W/dalvikvm(19642): chunk is too big to transmit (length=49398928, 48259 bytes)
08-26 17:15:37.282: E/dalvikvm-heap(19642): Out of memory on a 67108880-byte allocation.
08-26 17:15:37.282: I/dalvikvm(19642): "GLThread 14876" prio=5 tid=19 RUNNABLE
08-26 17:15:37.282: I/dalvikvm(19642):   | group="main" sCount=0 dsCount=0 obj=0x42c120d8 self=0x787e8438
08-26 17:15:37.282: I/dalvikvm(19642):   | sysTid=19729 nice=1 sched=0/0 cgrp=apps handle=2021558416
08-26 17:15:37.282: I/dalvikvm(19642):   | state=R schedstat=( 54770836691 14106993077 84458 ) utm=1996 stm=3481 core=0
08-26 17:15:37.282: I/dalvikvm(19642):   at android.graphics.Bitmap.nativeCreate(Native Method)
08-26 17:15:37.282: I/dalvikvm(19642):   at android.graphics.Bitmap.createBitmap(Bitmap.java:924)
08-26 17:15:37.282: I/dalvikvm(19642):   at android.graphics.Bitmap.createBitmap(Bitmap.java:901)
08-26 17:15:37.282: I/dalvikvm(19642):   at android.graphics.Bitmap.createBitmap(Bitmap.java:868)
08-26 17:15:37.282: I/dalvikvm(19642):   at opl.a((null):-1)
08-26 17:15:37.282: I/dalvikvm(19642):   at okg.a((null):-1)
08-26 17:15:37.282: I/dalvikvm(19642):   at opy.a((null):-1)
08-26 17:15:37.282: I/dalvikvm(19642):   at opx.a((null):-1)
08-26 17:15:37.282: I/dalvikvm(19642):   at ojx.a((null):-1)
08-26 17:15:37.282: I/dalvikvm(19642):   at ojx.b((null):-1)
08-26 17:15:37.282: I/dalvikvm(19642):   at otq.k((null):-1)
08-26 17:15:37.282: I/dalvikvm(19642):   at otq.run((null):-1)
08-26 17:15:37.432: W/dalvikvm(19642): threadid=19: thread exiting with uncaught exception (group=0x4178cda0)
08-26 17:15:37.442: E/AndroidRuntime(19642): FATAL EXCEPTION: GLThread 14876
08-26 17:15:37.442: E/AndroidRuntime(19642): Process: com.example.maptest, PID: 19642
08-26 17:15:37.442: E/AndroidRuntime(19642): java.lang.OutOfMemoryError
08-26 17:15:37.442: E/AndroidRuntime(19642):    at android.graphics.Bitmap.nativeCreate(Native Method)
08-26 17:15:37.442: E/AndroidRuntime(19642):    at android.graphics.Bitmap.createBitmap(Bitmap.java:924)
08-26 17:15:37.442: E/AndroidRuntime(19642):    at android.graphics.Bitmap.createBitmap(Bitmap.java:901)
08-26 17:15:37.442: E/AndroidRuntime(19642):    at android.graphics.Bitmap.createBitmap(Bitmap.java:868)
08-26 17:15:37.442: E/AndroidRuntime(19642):    at opl.a(Unknown Source)
08-26 17:15:37.442: E/AndroidRuntime(19642):    at okg.a(Unknown Source)
08-26 17:15:37.442: E/AndroidRuntime(19642):    at opy.a(Unknown Source)
08-26 17:15:37.442: E/AndroidRuntime(19642):    at opx.a(Unknown Source)
08-26 17:15:37.442: E/AndroidRuntime(19642):    at ojx.a(Unknown Source)
08-26 17:15:37.442: E/AndroidRuntime(19642):    at ojx.b(Unknown Source)
08-26 17:15:37.442: E/AndroidRuntime(19642):    at otq.k(Unknown Source)
08-26 17:15:37.442: E/AndroidRuntime(19642):    at otq.run(Unknown Source)
08-26 17:15:37.522: D/AbsListView(19642): unregisterIRListener() is called 
08-26 17:15:38.092: D/AbsListView(19642): onDetachedFromWindow
08-26 17:15:38.092: D/AbsListView(19642): unregisterIRListener() is called 
08-26 17:15:38.102: D/AbsListView(19642): onVisibilityChanged() is called, visibility : 8
08-26 17:15:38.102: D/AbsListView(19642): unregisterIRListener() is called

@user3249477 - 在设备上运行。在GS4和GS5上都会崩溃。 - spidermonkey
哦,算了,我没有看到“激烈的地图平移和缩放约2分钟”的部分... - Simas
我在GS4/4.4.2上遇到了同样的问题 java.lang.OutOfMemoryError 在android.graphics.Bitmap.nativeCreate(本机方法)中 在android.graphics.Bitmap.createBitmap(Bitmap.java:903)中 在android.graphics.Bitmap.createBitmap(Bitmap.java:880)中 在android.graphics.Bitmap.createBitmap(Bitmap.java:847)中 在opl.a(未知来源)中 在okg.a(未知来源)中 在opy.a(未知来源)中 在opx.a(未知来源)中 在ojx.a(未知来源)中 在ojx.b(未知来源)中 在otq.k(未知来源)中 在otq.run(未知来源)中 - david
我认为你可能正在经历和我一样的问题。我已经在Google Maps Android API跟踪器上开了一个错误报告:https://code.google.com/p/gmaps-api-issues/issues/detail?id=7325 - Zeb Barnett
2个回答

0
尝试在相应的Fragment方法中调用map onPause、onResume、onLowMemory。

添加了方法,但仍然遇到OOM。onLowMemory仅在“整个系统即将耗尽内存而不是您的进程即将耗尽内存”时调用。在此情况下,由于用户从未离开地图,因此不会调用onPause和onResume。 - spidermonkey

0

我在尝试将应用程序运行在演示模式下几个小时后遇到了这个问题。无论我尝试什么,30分钟后,我都会看到这个崩溃,但没有可读的堆栈报告。

我尝试了很多不同的方法,但这是唯一对我有效的方法。虽然它不能修复地图错误,但它可以防止我的应用程序崩溃:

    <application
    ...
    android:largeHeap="true">

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接