0

I'm building a hybrid android app that opens up a webview and inside of it creating and removing iframes.

As part of memory optimizations, I isolated only the creation and removal of the iframe and found out that iframe is not released in the webview, while in the browser it works well.

I did the Following test twice. First, on Chrome browser (Chrome v39). Second, on Google Nexus 4 (KitKat 4.4).

The test:

  1. Run the html as written in jsbin: http://jsbin.com/yubuvokawa/1/
  2. Start recording timeline in devtools.
  3. Click Create.
  4. Click Remove.
  5. Garbage collection.
  6. Stop Recording.

These are the results as shown in the Chrome browser:

Iframe is created and destroyed and goes back to normal (except additional JS heap)

https://i.stack.imgur.com/vz1Tt.png

These are the results as shown in the Nexus 4 webview:

As you can see, when the iframe is released, the document is still alive (the red line) as well as some unreleased nodes

https://i.stack.imgur.com/Qnt8d.png

This is the piece of code I'm using to open a webview:

webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new MyWebViewClient());

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    WebView.setWebContentsDebuggingEnabled(true);
}

String url = getIntent().getExtras().getString("url");
Log.w(getClass().getSimpleName(), "Loading url: " + url);
webView.loadUrl(url);

What can cause the difference? how should I clean the iframe from the webview?

ShlomiTC
  • 446
  • 4
  • 12
  • Make sure no references are kept anywhere to the element and let the GC handle it. You can't force the GC without doing stupid things like deliberately filling the memory with trash data and even then you have no real guarantees. Possibly http://stackoverflow.com/questions/3715482/clear-webview-content has something useful? – G_V Jan 22 '15 at 11:10
  • @G_V - I am not forcing the native GC, I'm using the one in the devtools which is legit it terms of checking memory consumption. Also, I am using only one live webview during my app's life, and not trying to kill it and create another one. No other JS references are kept to the iframe rather than the ones in the html code – ShlomiTC Jan 22 '15 at 11:44

0 Answers0