How to destroy a WebView from memory (iOS)

You must Login before you can answer or comment on any questions.

Hi,

I'm trying to remove a webView from memory, removing from its parent component and assigning null value to it.

This works with any other component in the same view, but not for the webView. I've tested with xCode profile tools. Using Allocations tools, I see how the rest of components go to transitory when I manually unload them. But the webview remains "Living". If I load the view again, a new webView is created, with an important memory leak.

Am I doing something wrong? How can I achieve this?

this is the module where I create the parent View, I've added a public 'unload' method for testing purpposes

module.exports = function (args) {
 
    var args = argsĀ || {};
 
    var view = Ti.UI.createView(Styles.FullVIew);
 
    var titleView = new TitleModule('Section title');
 
    var webView = Ti.UI.createWebView({
        top:titleView.height,
        right:45, left:460, bottom:0,
        url:'whatever.html'
    });
 
    var images = [
        '/images/img-1.jpg',
        '/images/img-2.jpg',
        '/images/img-3.jpg'
    ];
 
    var gallery = new GalleryModule({
        top:titleView.height, left:20, width:400, bottom:10,
        itemHeight:202,
        itemMargin:10,
        imgs:images
    });
 
 
    view.add(titleView);
    view.add(webView);
    view.add(gallery);
 
    //optional public function to unload components when module is hide and liberate memory
    view.unload = function(){
        view.remove(titleView);
        view.remove(webView);
        view.remove(gallery);
 
        viewTitle = null;
        webView = null;  //does nothing???
        gallery = null;
        //view = null;  //tried this, but is the same
    }
 
    return view;
}
testing on iOS 5, xCode 4.2.1, MacOS 10.7.3

2 Answers

Try to set the html property to null before nullifying the webview. maybe this will free the content of the webview.

— answered 2 years ago by Dan Tamas
answer permalink
7 Comments
  • nop :( assigning null to html throws an exception. I tried an empty string instead, but the webView component is still alive. Does not jump to the "transitory" column.

    — commented 2 years ago by Javier Rayon

  • buggy bug maybe? :)

    — commented 2 years ago by Dan Tamas

  • Maybe, but is hard to confirm. I'll try the MemoryPool technique and I'll see what conclusion I have.

    — commented 2 years ago by Javier Rayon

  • Show 4 more comments

Whenever I ran across memory issues, the following post helped me clear most if not all of them up:

http://developer.appcelerator.com/question/116867/this-is-a-solution-to-your-memory-woes

Your Answer

Think you can help? Login to answer this question!