WebView eating up too much memory

前端 未结 1 965
生来不讨喜
生来不讨喜 2021-01-03 02:22

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

Activity A:

It has a list which displays information about an item briefly. When an item on

相关标签:
1条回答
  • 2021-01-03 02:58

    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;
            }
    }
    

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

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        releaseWebViews();
    }
    
    0 讨论(0)
提交回复
热议问题