xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/loading_bg"
android:orientation="vertical">
<FrameLayout
android:id="@+id/lLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/loading_bg"
android:orientation="vertical">
<com.xxx.xxx.app.web.MySurfaceView
android:id="@+id/sv"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</com.xxx.xxx.app.web.MySurfaceView >
......
java 代碼:
.....
mSurfaceView = (MySurfaceView) mMainView.findViewById(R.id.sv);
SurfaceHolder sh = mSurfaceView.getHolder();
sh.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
sh.addCallback(this);
.....
MySurfaceView
public class MySurfaceView extends SurfaceView {
private static final String TAG ="BesTVSurfaceView";
public MySurfaceView (Context context) {
super(context);
}
public MySurfaceView (Context context, AttributeSet attrs) {
super(context, attrs);
}
public MySurfaceView (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
private void initView() {
Log.d(TAG,"initView");
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
initView();
}
}
LeakCanary 記憶體泄漏分析工具
結果日志:
* com.xxx.xxx.app.MainActivity has leaked:
* GC ROOT static android.view.SurfaceView.mSurfaceContext
* leaks com.xxx.xxx.app.MainActivity instance[/code]
uj5u.com熱心網友回復:
msurfacecontext泄漏,因為它是靜態的,估計你的activity的launchmode是標準,而非single instance,每次都生成新頁面而沒能回收。uj5u.com熱心網友回復:
應該是你的SurfaceHokder 的 addCallback(this),導致的記憶體泄漏建議頁面銷毀時removeCallback
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/111330.html
標籤:Android
