SupportMapFragment 正在泄漏内存
SupportMapFragment is leaking Memory
我正在试用新版 GoogleMap
com.google.android.libraries.maps:maps:3.1.0-beta
但是当我回到我以前的时候它似乎会泄漏内存Activity
我在 Activity
中使用 Fragment
代码如下:
XML
<androidx.fragment.app.FragmentContainerView
android:id="@+id/map"
android:name="com.google.android.libraries.maps.SupportMapFragment"
android:layout_width="0dp"
android:layout_height="220dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:useViewLifecycle="true"
tools:context=".OutputScreen"/>
Java代码
public class OutputScreen extends FragmentActivity implements OnMapReadyCallback{
private GoogleMap map;
private SupportMapFragment mapFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setting layout
mapFragment = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
}
@Override
protected void onDestroy() {
if (map != null){
map.clear();
map.setMyLocationEnabled(false)
map = null;
}
}
}
LeakCanary 报告
┬───
│ GC Root: System class
│
├─ android.provider.FontsContract class
│ Leaking: NO (Application↓ is not leaking and a class is never leaking)
│ ↓ static FontsContract.sContext
├─ android.app.Application instance
│ Leaking: NO (Application is a singleton)
│ Application does not wrap an activity context
│ ↓ Application.mActivityLifecycleCallbacks
│ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
├─ java.util.ArrayList instance
│ Leaking: UNKNOWN
│ ↓ ArrayList.elementData
│ ~~~~~~~~~~~
├─ java.lang.Object[] array
│ Leaking: UNKNOWN
│ ↓ Object[].[5]
│ ~~~
├─ com.google.android.libraries.maps.dj.zzf instance
│ Leaking: UNKNOWN
│ ↓ zzf.zza
│ ~~~
├─ com.google.android.libraries.maps.dj.zzd instance
│ Leaking: UNKNOWN
│ ↓ zzd.zzd
│ ~~~
├─ com.google.android.libraries.maps.bm.zzu instance
│ Leaking: UNKNOWN
│ ↓ zzu.zzd
│ ~~~
├─ com.google.android.libraries.maps.kf.zzab instance
│ Leaking: UNKNOWN
│ ↓ zzab.zzh
│ ~~~
├─ com.google.android.libraries.maps.kf.zzd instance
│ Leaking: UNKNOWN
│ ↓ zzd.zzi
│ ~~~
├─ com.google.android.libraries.maps.ed.zzau instance
│ Leaking: YES (View detached and has parent)
│ mContext instance of android.app.Application, not wrapping activity
│ View#mParent is set
│ View#mAttachInfo is null (view detached)
│ View.mWindowAttachCount = 1
│ ↓ zzau.mParent
├─ android.widget.FrameLayout instance
│ Leaking: YES (zzau↑ is leaking and View detached and has parent)
│ mContext instance of android.app.Application, not wrapping activity
│ View#mParent is set
│ View#mAttachInfo is null (view detached)
│ View.mWindowAttachCount = 1
│ ↓ FrameLayout.mParent
╰→ android.widget.FrameLayout instance
Leaking: YES (ObjectWatcher was watching this because com.google.android.libraries.maps.SupportMapFragment received Fragment#onDestroyView() callback (references to its views should be cleared to prevent leaks) and View.mContext references a destroyed activity)
我查看了其他 Whosebug 答案,但没有找到任何合适的解决方案。
问题是 SupportMapFragment 已合并到您的 Fragment 容器视图中,但未调用 ondestroy。您需要手动调用它。
在你的 OutputScreen class onDestroy() 方法下,你需要调用如下:
mapFragment.onDestroy() // call this in your activity's onDestroy
我正在试用新版 GoogleMap
com.google.android.libraries.maps:maps:3.1.0-beta
但是当我回到我以前的时候它似乎会泄漏内存Activity
我在 Activity
中使用 Fragment代码如下:
XML
<androidx.fragment.app.FragmentContainerView
android:id="@+id/map"
android:name="com.google.android.libraries.maps.SupportMapFragment"
android:layout_width="0dp"
android:layout_height="220dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:useViewLifecycle="true"
tools:context=".OutputScreen"/>
Java代码
public class OutputScreen extends FragmentActivity implements OnMapReadyCallback{
private GoogleMap map;
private SupportMapFragment mapFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setting layout
mapFragment = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
}
@Override
protected void onDestroy() {
if (map != null){
map.clear();
map.setMyLocationEnabled(false)
map = null;
}
}
}
LeakCanary 报告
┬───
│ GC Root: System class
│
├─ android.provider.FontsContract class
│ Leaking: NO (Application↓ is not leaking and a class is never leaking)
│ ↓ static FontsContract.sContext
├─ android.app.Application instance
│ Leaking: NO (Application is a singleton)
│ Application does not wrap an activity context
│ ↓ Application.mActivityLifecycleCallbacks
│ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
├─ java.util.ArrayList instance
│ Leaking: UNKNOWN
│ ↓ ArrayList.elementData
│ ~~~~~~~~~~~
├─ java.lang.Object[] array
│ Leaking: UNKNOWN
│ ↓ Object[].[5]
│ ~~~
├─ com.google.android.libraries.maps.dj.zzf instance
│ Leaking: UNKNOWN
│ ↓ zzf.zza
│ ~~~
├─ com.google.android.libraries.maps.dj.zzd instance
│ Leaking: UNKNOWN
│ ↓ zzd.zzd
│ ~~~
├─ com.google.android.libraries.maps.bm.zzu instance
│ Leaking: UNKNOWN
│ ↓ zzu.zzd
│ ~~~
├─ com.google.android.libraries.maps.kf.zzab instance
│ Leaking: UNKNOWN
│ ↓ zzab.zzh
│ ~~~
├─ com.google.android.libraries.maps.kf.zzd instance
│ Leaking: UNKNOWN
│ ↓ zzd.zzi
│ ~~~
├─ com.google.android.libraries.maps.ed.zzau instance
│ Leaking: YES (View detached and has parent)
│ mContext instance of android.app.Application, not wrapping activity
│ View#mParent is set
│ View#mAttachInfo is null (view detached)
│ View.mWindowAttachCount = 1
│ ↓ zzau.mParent
├─ android.widget.FrameLayout instance
│ Leaking: YES (zzau↑ is leaking and View detached and has parent)
│ mContext instance of android.app.Application, not wrapping activity
│ View#mParent is set
│ View#mAttachInfo is null (view detached)
│ View.mWindowAttachCount = 1
│ ↓ FrameLayout.mParent
╰→ android.widget.FrameLayout instance
Leaking: YES (ObjectWatcher was watching this because com.google.android.libraries.maps.SupportMapFragment received Fragment#onDestroyView() callback (references to its views should be cleared to prevent leaks) and View.mContext references a destroyed activity)
我查看了其他 Whosebug 答案,但没有找到任何合适的解决方案。
问题是 SupportMapFragment 已合并到您的 Fragment 容器视图中,但未调用 ondestroy。您需要手动调用它。
在你的 OutputScreen class onDestroy() 方法下,你需要调用如下:
mapFragment.onDestroy() // call this in your activity's onDestroy