记录一次腾讯X5Webview内存泄漏
2020-08-11 本文已影响0人
TMAC_EAH
In com.sinochem.farm:3.1.6:13.
* androidx.lifecycle.ReportFragment has leaked:
* WebContentsObserverProxy.!(d)!
* ↳ h.!(a)!
* ↳ ArrayList.!(elementData)!
* ↳ array Object[].!([1])!
* ↳ WebViewChromiumExtension$WebContentsObserverExtension.!(a)!
* ↳ WebViewChromiumExtension.!(i)!
* ↳ X5WebViewAdapter.!(b)!
* ↳ BaseWebActivity.mFragments
* ↳ FragmentController.mHost
* ↳ Activity$HostCallbacks.mFragmentManager
* ↳ FragmentManagerImpl.mAdded
* ↳ ArrayList.elementData
* ↳ array Object[].[0]
* ↳ ReportFragment
* Reference Key: 15f37e3d-47f3-4fc9-8090-9af903c3ef44
* Device: HUAWEI HONOR PCT-AL10 PCT-AL10
* Android Version: 10 API: 29 LeakCanary: 1.6.2 0ebc1fc
* Durations: watch=5034ms, gc=164ms, heap dump=1965ms, analysis=38701ms
* Details:
* Instance of org.chromium.content.browser.webcontents.WebContentsObserverProxy
| static $class$dexTypeIndex = 5135
| static $class$componentType = null
| static $class$iFields = 2704830752
| static $class$primitiveType = 131072
| static $class$ifTable = java.lang.Object[0]@1882063536 (0x702e02b0)
| static $class$status = -536870912
| static $class$clinitThreadId = 2177
| static $class$accessFlags = 524288
| static $class$classFlags = 0
| static a = true
| static $class$extData = null
| static $class$shadow$_monitor_ = 0
| static $class$virtualMethodsOffset = 4
| static $class$sFields = 2704830728
| static $class$methods = 2704830808
| static $class$numReferenceInstanceFields = 2
| static $class$referenceInstanceOffsets = 7
| static $class$classLoader = dalvik.system.DexClassLoader@317457944 (0x12ec0618)
| static $class$classSize = 289
| static $class$objectSize = 32
| static $class$superClass = org.chromium.content_public.browser.z
| static $classOverhead = byte[164]@317467689 (0x12ec2c29)
| static $class$copiedMethodsOffset = 33
| static $class$vtable = null
| static $class$objectSizeAllocFastPath = 32
| static $class$name = null
| static $class$numReferenceStaticFields = 0
| static $class$dexCache = java.lang.DexCache@317485664 (0x12ec7260)
| static $class$shadow$_klass_ = java.lang.Class
| static $class$dexClassDefIndex = 4297
| c = -443753536
| d = org.chromium.base.h@320958936 (0x132171d8)
| e = org.chromium.base.h$a@320958960 (0x132171f0)
| b = null
| shadow$_klass_ = org.chromium.content.browser.webcontents.WebContentsObserverProxy
| shadow$_monitor_ = 0
* Instance of org.chromium.base.h
| static $class$dexTypeIndex = 4624
| static $class$componentType = null
| static $class$iFields = 3137982920
| static $class$primitiveType = 131072
| static $class$ifTable = java.lang.Object[2]@320883848 (0x13204c88)
| static $class$status = -536870912
| static $class$clinitThreadId = 2177
| static $class$accessFlags = 524289
| static $class$classFlags = 0
| static b = true
| static $class$extData = null
| static $class$shadow$_monitor_ = 0
| static $class$virtualMethodsOffset = 11
| static $class$sFields = 3137982896
| static $class$methods = 3137982992
| static $class$numReferenceInstanceFields = 1
| static $class$referenceInstanceOffsets = 1
| static $class$classLoader = dalvik.system.DexClassLoader@317457944 (0x12ec0618)
| static $class$classSize = 209
| static $class$objectSize = 21
| static $class$superClass = java.lang.Object
| static $classOverhead = byte[84]@317573561 (0x12edc9b9)
| static $class$copiedMethodsOffset = 18
| static $class$vtable = null
| static $class$objectSizeAllocFastPath = 24
| static $class$name = null
| static $class$numReferenceStaticFields = 0
| static $class$dexCache = java.lang.DexCache@317485664 (0x12ec7260)
| static $class$shadow$_klass_ = java.lang.Class
| static $class$dexClassDefIndex = 2610
| a = java.util.ArrayList@320958984 (0x13217208)
| c = 0
| d = 2
| e = false
| shadow$_klass_ = org.chromium.base.h
| shadow$_monitor_ = 0
* Instance of java.util.ArrayList
| static $class$dexTypeIndex = 1430
| static EMPTY_ELEMENTDATA = java.lang.Object[0]@1878049872 (0x6ff0c450)
| static $class$componentType = null
| st
解决方案:
一顿百度,最终结果就是这个,浪费很久时间,这个能解决内存泄漏,我走了些弯路
public void onDestroy() {
if (x5WebView != null) {
// x5WebView.clearCache(true);
x5WebView.stopLoading();
x5WebView.removeAllViewsInLayout();
x5WebView.removeAllViews();
x5WebView.setWebViewClient(null);
try {
CookieSyncManager.getInstance().stopSync();
} catch (Exception e) {
e.printStackTrace();
}
x5WebView.destroy();
x5WebView = null;
}
super.onDestroy();
}
项目中用 ButterKnife 我在onDestroyView解绑定,x5WebView 会置 null,导致onDestroy 收尾工作无法正常执行,从而导致内存泄漏。
mUnbinder = ButterKnife.bind(this, view);//ButterKnife注解
@Override
public void onDestroyView() {
super.onDestroyView();
//-----------------------解绑提前了,导致内存泄漏
}
@Override
public void onDetach() {
// 移到这里解绑即可解决问题
if (mUnbinder != null) {
mUnbinder.unbind();
}
super.onDetach();
}
生命周期如下,啧啧,多亏了debug发现xwebview意料之外变null了,不然,我还得百度,并且还查不到问题~~
2020-08-11 14:31:07.612 20841-20841/ E/:::::::::BaseWebFragment:::::::: BaseWebFragment===================>>onAttach
2020-08-11 14:31:07.612 20841-20841/ E/:::::::::BaseWebFragment:::::::: BaseWebFragment===================>>onCreate
2020-08-11 14:31:07.775 20841-20841/ E/:::::::::BaseWebFragment:::::::: BaseWebFragment===================>>onViewCreated
2020-08-11 14:31:07.775 20841-20841/ E/:::::::::BaseWebFragment:::::::: BaseWebFragment===================>>onActivityCreated
2020-08-11 14:31:07.775 20841-20841/ E/:::::::::BaseWebFragment:::::::: BaseWebFragment===================>>onStart
2020-08-11 14:31:07.778 20841-20841/ E/:::::::::BaseWebFragment:::::::: BaseWebFragment========onResume=========>程序运行时间:166毫秒
2020-08-11 14:31:07.778 20841-20841/ E/:::::::::BaseWebFragment:::::::: BaseWebFragment===================>>onResume
2020-08-11 14:31:15.307 20841-20841/ E/:::::::::BaseWebFragment:::::::: BaseWebFragment===================>>onPause
2020-08-11 14:31:15.533 20841-20841/ E/:::::::::BaseWebFragment:::::::: BaseWebFragment===================>>onStop
2020-08-11 14:31:15.536 20841-20841/ E/:::::::::BaseWebFragment:::::::: BaseWebFragment===================>>onDestroyView
2020-08-11 14:31:15.545 20841-20841/ W/System.err: at com.sinochem.gardencrop.web.BaseWebFragment.destroyWebView(BaseWebFragment.java:235)
2020-08-11 14:31:15.545 20841-20841/ W/System.err: at com.sinochem.gardencrop.web.BaseWebFragment.onDestroy(BaseWebFragment.java:219)
2020-08-11 14:31:15.558 20841-20841/ W/System.err: at com.sinochem.gardencrop.web.BaseWebFragment.destroyWebView(BaseWebFragment.java:248)
2020-08-11 14:31:15.558 20841-20841/ W/System.err: at com.sinochem.gardencrop.web.BaseWebFragment.onDestroy(BaseWebFragment.java:219)
2020-08-11 14:31:15.559 20841-20841/ E/:::::::::BaseWebFragment:::::::: BaseWebFragment===================>>onDestroy
2020-08-11 14:31:15.559 20841-20841/ E/:::::::::BaseWebFragment:::::::: BaseWebFragment===================>>onDetach