GC Root: Global variable in native code ─ dalvik.system.PathClassLoader instance Leaking: NO (NetSpeedManager$b↓ is not leaking and A ClassLoader is never leaking) ↓ ClassLoader.runtimeInternalObjects ─ java.lang.Object[] array Leaking: NO (NetSpeedManager$b↓ is not leaking) ↓ Object[1626] ─ com.konka.apkhall.edu.module.album.player.presenter.NetSpeedManager$b class Leaking: NO (a class is never leaking) ↓ static NetSpeedManager$b.a ~ ─ com.konka.apkhall.edu.module.album.player.presenter.NetSpeedManager instance Leaking: UNKNOWN Retaining 6.7 MB in 75213 objects ↓ NetSpeedManager.listeners ~~~~~~~~~ ─ java.util.Collections$SynchronizedRandomAccessList instance Leaking: UNKNOWN Retaining 6.7 MB in 75212 objects ↓ Collections$SynchronizedCollection.c ~ ─ java.util.ArrayList instance Leaking: UNKNOWN Retaining 6.7 MB in 75211 objects ↓ ArrayList[8] ~~~ ─ com.konka.apkhall.edu.module.album.player.widgets.VideoStatusView instance Leaking: YES (View.mContext references a destroyed activity) Retaining 1.3 MB in 15235 objects View not part of a window view hierarchy View.mAttachInfo is null (view detached) View.mID = R.id.video_status View.mWindowAttachCount = 1 mContext instance of com.konka.apkhall.edu.module.album.AlbumActivity with mDestroyed = true ↓ View.mContext → com.konka.apkhall.edu.module.album.AlbumActivity instance Leaking: YES (ObjectWatcher was watching this because com.konka.apkhall.edu.module.album.AlbumActivity received Activity#onDestroy() callback and Activity#mDestroyed is true) Retaining 1.0 MB in 11484 objects key = 9ca61e77-8cfc-471d-94d2-e64c3b173343 watchDurationMillis = 32423 retainedDurationMillis = 27421 mApplication instance of com.konka.apkhall.edu.app.EduApplication mBase instance of android.app.ContextImpl分析原因,给出具体解决代码
时间: 2023-11-27 22:53:54 浏览: 202
根据分析结果,有一个名为`NetSpeedManager`的类实例被保留,其中包含一个`VideoStatusView`对象实例,该对象引用了一个已销毁的`AlbumActivity`实例,并导致该`AlbumActivity`实例无法被垃圾回收器回收。
解决方法是在`AlbumActivity`的`onDestroy()`方法中取消对该`VideoStatusView`对象实例的引用,例如:
```
@Override
protected void onDestroy() {
super.onDestroy();
videoStatusView.setActivityDestroyed(true); // 设置标志位
videoStatusView = null; // 取消引用
}
```
在`VideoStatusView`中添加一个标志位`activityDestroyed`,并在其`onDetachedFromWindow()`方法中检查该标志位,如果为`true`则取消对`mContext`的引用,例如:
```
public class VideoStatusView extends FrameLayout {
private boolean activityDestroyed = false; // 标志位
// ...
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (activityDestroyed) {
mContext = null; // 取消引用
}
}
public void setActivityDestroyed(boolean destroyed) {
activityDestroyed = destroyed;
}
}
```
阅读全文