本文介绍了吃的WebView占用太多内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序有两个活动,即A和B。

In my app there are two activities viz A and B.

活动答:

它具有简单显示有关项目的信息列表。当点击了一个项目,它加载活动B

It has a list which displays information about an item briefly. When an item on A is clicked, it loads Activity B.

活动B:

它有两个网站的观点和几个列表视图。活动的顶部由头部Web视图占据。活动的下部有标签的主机,这个主机在第一个选项卡和剩余标签中的第二个网络视图中的每个主机的列表视图。

It has two web views and several listviews. Top of the activity is occupied by header web view. Lower part of activity has tab host, this hosts the second web view in first tab and remaining tab each hosts a list view.

问题:

当用户从导航到B,堆大小增加显著。即使在我从B到A导航回来,堆的大小仍然是相同的。竟然没有一个字节的下降,事实上,它有时会增加。是的,因为这些网站的意见。我已阅读有关SO和其他网站的Web视图内存泄漏​​。我也跟着提到的办法here

When user navigates from A to B, the heap size increases significantly. Even after I navigate back from B to A, heap size continues to be the same. There is not even a byte decrease, in fact it increases sometimes. Yes its because of these web views. I have read about web view memory leaks on SO and other sites. I followed approach mentioned here


  1. 动态添加Web视图的容器[RelativeLayout的]中。在活动中删除的WebView的onDestroy

  2. 作为SO文章中建议子归入Web视图

  3. 清除网络视图缓存每次等

不管是什么堆大小不回来什么是导航到B前面。

No matter what heap size does not come back to what it was before navigating to B.

真的AP preciate如果有人能指导我一个可能的修复

Really appreciate if someone can guide me to a possible fix

注意:

我已经阅读并遵循对SO这个问题。
在WebView中

I have already read this and followed this issue on SO.Memory leak in WebView

编辑:

我曾尝试无B织物的意见,并在堆大小的增加是非常非常少约0.5 MB但随着Web视图会起皱4-5 MB

I have tried without web views in B and the increase in heap size is very very less around 0.5 MB but with web views it creases by 4-5 MB

堆大小日志(按照提到的建议here )

Heap size logs ( got by following suggestion mentioned here )

onCreate B
debug.heap native: allocated 4.11MB of 4.17MB (0.01MB free) in []
debug.memory: allocated: 12.00MB of 96.00MB (1.00MB free)


onDestroy B
debug.heap native: allocated 8.66MB of 10.08MB (0.48MB free) in []
debug.memory: allocated: 12.00MB of 96.00MB (1.00MB free)

on Resume A
debug.heap native: allocated 7.94MB of 10.08MB (1.32MB free) in []
debug.memory: allocated: 12.00MB of 96.00MB (0.00MB free)

我过通过采取堆转储之前和B启动并destoryed后,堆的大小是相当接近一个我得到它在日志中签号码

I cross checked the numbers by taking heap dump before and after B is started and destoryed, heap size are quite close to the one I get it in logs

推荐答案

我遵循一系列的步骤,得到了内存占用减少。这是我做的,
而不是通过XML创建静态的WebView我现在编程方式创建的WebView并将其添加到一个容器。一旦web视图不再需要我从容器中取出的WebView然后我删除的WebView各方意见,呼吁消灭它。最终分配的内存减少了。

I followed series of steps and got the memory footprint reduced. This is what I did,instead of creating webview statically via xml I now create webview programmatically and add it to a container. Once webview is no longer needed I remove the webview from the container then I remove all views from webview and call destroy on it. Eventually memory allocated reduces.

private void releaseWebView() {

    webViewContainerRelView.removeAllViews();
        if(mWebView != null){
            mWebView.setTag(null);
            mWebView.clearHistory();
            mWebView.removeAllViews();
            mWebView.clearView();
            mWebView.destroy();
            mWebView = null;
        }
}

我叫releaseWebView()从如下活动onDetachedFromWindow方法。

I call releaseWebView() from onDetachedFromWindow method of Activity as below.

@Override
public void onDetachedFromWindow() {
    super.onDetachedFromWindow();
    releaseWebViews();
}

这篇关于吃的WebView占用太多内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 09:23