Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-13250] Android: Memory leak in Ti.UI.WebView

GitHub Issuen/a
TypeBug
PriorityMedium
StatusClosed
ResolutionCannot Reproduce
Resolution Date2013-05-03T22:44:19.000+0000
Affected Version/sn/a
Fix Version/s2013 Sprint 09 Core, 2013 Sprint 09
ComponentsAndroid
Labelsexalture, triage
ReporterCarter Lathrop
AssigneeAllen Yeung
Created2013-03-06T14:48:33.000+0000
Updated2017-03-21T22:15:26.000+0000

Description

When closing a webview, memory is not released properly. Removing the webview and nulling variables does not solve the problem. App.js:
(function() {	
	var w = Ti.UI.createWindow();
	w.exitOnClose = true;
	w.setBackgroundColor("red");
	w.setNavBarHidden(true);
	
	var b = Ti.UI.createButton();
	b.setTitle("Press me");
	b.addEventListener('click', function()
	{
	   var w2 = Ti.UI.createWindow();
	   w2.setModal(true);
	   w2.setNavBarHidden(true);
	   w2.setBackgroundColor("white");
	   
	   var l = Titanium.UI.createWebView({
		  html:'hello world'
	   });
	   w2.add(l);
	   w2.addEventListener('android:back', function(e){
	   	   w2.remove(l);  
	   	   l = null;
	   	   w2.close();	  
	   	   w2 = null; 	  
		});
	   w2.open();
	});
	
	w.add(b);	
	w.open();	
})();
Steps to reproduce: 1) Launch app and check memory used (aprox 16mb) 2) Click the button to open the new window with the webview 3) Close the window with back button and check memory used (22mb) 4) Repeat steps 1-3. Every time window with webview is opened and closed, memory used is increased by aprox 1mb.

Comments

  1. javier ferrero 2013-03-11

    Adding another example that demonstrates the memory leak. Now adding the webview directly to the main window:
       var win = Ti.UI.createWindow({
           backgroundColor : 'white'
       });
       var webview = null;
       var btnAdd = Ti.UI.createButton({
           title : 'Add',
           top : 0,
           left : 0,
           height : 50,
           width : 100
       });
       btnAdd.addEventListener('click', function() {
           webview = Ti.UI.createWebView({
           	html : 'sample html',
               //url : 'local_webview.html',
               //url : 'http://www.google.com',
               height : 200,
               bottom : 10
           });    
           win.add(webview);
       });
       var btnRemove = Ti.UI.createButton({
           title : 'Remove',
           top : 0,
           right : 0,
           height : 50,
           width : 100
       });
       btnRemove.addEventListener('click', function() {
           win.remove(webview);
           webview = null;
       });
       win.add(btnAdd);
       win.add(btnRemove);
       win.open();
       
    Tested in 2.0.2.GA, 3.0.0.GA and 3.1.0
  2. Soumya Kanti Kar 2013-04-02

    We need to call webView.release() while closing the window to release the memory related to webview in Android. http://docs.appcelerator.com/titanium/latest/#!/api/Titanium.UI.WebView-method-release But in the example, we are not calling this API. Don't we need to call this API?
  3. Max Stepanov 2013-04-02

    Calling webView.release() helps in general. However TiWebViewBindings are still leaking. But everything is being released when related Activity is destroyed.
  4. Allen Yeung 2013-05-03

    I've tried both test cases multiple times and I can't reproduce the memory leak here. Occasionally I see the memory build up a little when going back and forth, but it eventually goes back down. I opened over 10 webviews, and when I look at the heap dump, I see 0-3 instances of the webview being retained. Keep in mind that this number will vary depending on GC lag. There's no guarantee that the webviews will be cleaned up as soon as you hit the gc button in ddms. Going to mark this as cannot reproduce. Feel free to reopen if there is further information that can help reproduce this issue.
  5. Lee Morris 2017-03-21

    Closing ticket as the issue cannot be reproduced and due to the above comments.

JSON Source