Home > Mobile >  Android use surfaceview memory leaks, a great god help look
Android use surfaceview memory leaks, a great god help look

Time:10-04

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" & gt;

<frameLayout
The android: id="@ + id/lLayout"
Android: layout_width="fill_parent"
Android: layout_height="fill_parent"
Android: background="@ drawable/loading_bg"
Android: orientation="vertical" & gt;

The android: id="@ + id/sv"
Android: layout_width="fill_parent"
Android: layout_height="fill_parent" & gt;


.




Java code:



 
.

MSurfaceView=(MySurfaceView) mMainView. The findViewById (R.i d.s v);
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 () {
The d (TAG, "initView");
}


@ Override
Protected void onAttachedToWindow () {
Super. OnAttachedToWindow ();
InitView ();
}
}












LeakCanary memory leak analysis tool
Results the log:
* com. XXX. XXX. App. MainActivity has leaked:
* the GC ROOT static android. View. SurfaceView. MSurfaceContext
* leaks com. XXX. XXX. App. MainActivity instance [/code]

CodePudding user response:

Msurfacecontext leak, because it is static, launchmode is a standard to estimate the your activity, rather than a single instance, generate new page every time and can't recycle,

CodePudding user response:

Should be your SurfaceHokder addCallback (this), as a result of the memory leak

Recommendations page removeCallback when destroyed
  • Related