webview内存泄露

前段时间在搞公司的项目时,一个帖子的功能,里面用的是webview,在检查内存的时候,发现内存泄露;
在网上查了一下资料,下面记录一下解决过程。

1.检查代码

确定已经在onDestroy调用了webview的释放方法,但是依然存在内存泄露

mWebView.removeAllViews();
mWebView.destroy();

2.尝试网上的一些方案

2.1

new一个而不是在.xml中定义webview节点(没懂为什么)

2.2

使用ApplicationContext而不是Activity的context(会出现一些奇怪的bug,放弃)

2.3

通过反射方法,手动删除引用(高版本android不支持反射,所以放弃此方法)

2.4 进程

为加载WebView的界面开启新进程,在该页面退出之后关闭这个进程。
没试过不知道行不行.

public void releaseAllWebViewCallback()
    {
        if(Build.VERSION.SDK_INT < 16)
        {
            try
            {
                Field field = WebView.class.getDeclaredField("mWebViewCore");
                field = field.getType().getDeclaredField("mBrowserFrame");
                field = field.getType().getDeclaredField("sConfigCallback");
                field.setAccessible(true);
                field.set(null, null);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            
        }
        else
        {
            try
            {
                Field sConfigCallback = Class.forName("android.webkit.BrowserFrame").getDeclaredField("sConfigCallback");
                if(sConfigCallback != null)
                {
                    sConfigCallback.setAccessible(true);
                    sConfigCallback.set(null, null);
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            
        }
    }

3.分析造成原因

参考
主要是webview的子view,AwContents内部注册了registerComponentCallbacks,导致其成员变量mComponentCallbacks持有了外部activity的引用
引用链为:
mComponentCallbacks->AwContents->WebView->Activity
检查源码

 org.chromium.android_webview.AwContents 

    @Override
    public void onAttachedToWindow() {
        if (isDestroyed()) return;
        if (mIsAttachedToWindow) {
            Log.w(TAG, "onAttachedToWindow called when already attached. Ignoring");
            return;
        }
        mIsAttachedToWindow = true;

        mContentViewCore.onAttachedToWindow();
        nativeOnAttachedToWindow(mNativeAwContents, mContainerView.getWidth(),
                mContainerView.getHeight());
        updateHardwareAcceleratedFeaturesToggle();

        if (mComponentCallbacks != null) return;
        mComponentCallbacks = new AwComponentCallbacks();
        mContext.registerComponentCallbacks(mComponentCallbacks);
    }

    @Override
    public void onDetachedFromWindow() {
        if (isDestroyed()) return;
        if (!mIsAttachedToWindow) {
            Log.w(TAG, "onDetachedFromWindow called when already detached. Ignoring");
            return;
        }
        mIsAttachedToWindow = false;
        hideAutofillPopup();
        nativeOnDetachedFromWindow(mNativeAwContents);

        mContentViewCore.onDetachedFromWindow();
        updateHardwareAcceleratedFeaturesToggle();

        if (mComponentCallbacks != null) {
            mContext.unregisterComponentCallbacks(mComponentCallbacks);
            mComponentCallbacks = null;
        }

        mScrollAccessibilityHelper.removePostedCallbacks();
    }

发现,系统已经在attach和detach中进行了注册和反注册,但是发现存在一行代码

 if (isDestroyed()) return;

当isDestroyed返回true,则无法执行反注册的方法
我们的activity退出的时候,都会主动调用 WebView.destroy() 方法,经过分析,destroy()的执行时间在onDetachedFromWindow之前,所以就会导致不能正常进行unregister()。

4.解决方案

在上面的分析中,既然是destroy先于onDetachedFromWindow执行,那么反过来让onDetachedFromWindow先执行就可以解决问题了
所以,在主动调用destroy()之前,把webview从它的parent上面移除掉。

ViewParent parent = mWebView.getParent();
if (parent != null) {
    ((ViewGroup) parent).removeView(mWebView);
}

mWebView.destroy();
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。