webview 内存泄露解决办法

最近开始使用leakCanary为app进行内存泄露的检测


遇到了webview.mContext导致activity内存泄露

(不过在android 6.0的机子上没有遇到这样的问题)


经过搜索,在http://bugly.qq.com/bbs/forum.php?mod=viewthread&tid=516 里找到了原因


面对这样的问题,我们暂时可以通过在layout中放置webview的地方用framelayout代替


    <FrameLayout
        android:id="@+id/webview_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </FrameLayout>


再动态将新建的webview加进framelayout中

frameLayout = (FrameLayout) findViewById(R.id.webview_container);
        webView = new WebView(getApplicationContext());
        frameLayout.addView(webView);

在activity onDestroy的时候将webview与activity解除关联,我们可以从文档看出原因

webview 的destroy文档

    /**
     * Destroys the internal state of this WebView. This method should be called
     * after this WebView has been removed from the view system. No other
     * methods may be called on this WebView after destroy.
     */
    public void destroy() {
        checkThread();
        mProvider.destroy();
    }
mProvider(WebviewProvider)的destroy文档

    /**
     * See {@link WebView#destroy()}.
     * As well as releasing the internal state and resources held by the implementation,
     * the provider should null all references it holds on the WebView proxy class, and ensure
     * no further method calls are made to it.
     */
    public void destroy();



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值