Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebView eating up too much memory

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 A is clicked, it loads Activity B.

Activity B:

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.

Problem:

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. Added web view dynamically inside a container[RelativeLayout]. Removed webview on activities onDestroy
  2. Sub classed web view as suggested in the SO article
  3. Cleared web view cache every time etc

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

Really appreciate if someone can guide me to a possible fix

Note:

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

Edit:

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

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)

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

like image 201
Jeevan Avatar asked Jul 19 '13 08:07

Jeevan


1 Answers

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();
}
like image 184
Jeevan Avatar answered Nov 08 '22 12:11

Jeevan