Massive Memory Leak in iOS Uiwebview

Memory Leaks with UIWebView and Javascript

I finally found some clue at what is happening and above all a workaround that I would like to share.

I can confirm that the simple inclusion of some javascript file was causing a memory leak on reload of the web view. I even tried building a file with the HTML content, then loading it into the UIWebView through loadRequest, and reloading it through reload; the leaks were always there. I will post a radar for that.

What saved me was using innerHTML to update the content of the web view. Instead of relying on reload or loadHTMLString, I initialized my web view with an empty body (I mean, the head section was there, including all required JS/CSS files) then updated it setting document.body.innerHTML:

body = [body stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"setBody(\"%@\");", body]];

with setBody defined like this:

var setBody = function(body) {
document.body.innerHTML = body;
}

I obtained two benefits: the web view update became really fast (this is an effect of not updating the DOM, which on the other hand is not entirely desirable on the whole), and there were no memory leaks were running the app under Instruments. The drawback was that I had to fine-tune a couple of conditions where the app was running fine; specifically:

  1. loading the web view (even with an empty body page) take a lot, so you have to synchronize the first update of its content to when the DOM is ready;

  2. webViewDidFinishLoading seems not reliable: it is executed before document.readyState becomes complete;

  3. document.documentElement.height, the official way of retrieving the page height seems not reliable, too: workaround is getting the "computed style" of the body part and read its height value.

Hope this helps someone else who finds that his web views are leaking memory.

Freeing iOS UIWebView resources after usage

It's been several months since I asked this question and it seems no one knows any work around, so I'm just answering myself to give it some closure and, sadly, bad news to whoever is having this same issue.

We kept trying to fight this issue for several days and, after talking to some people who somehow hinted us that we had nothing to do about it (it's just a different process taking care of everything to which we have no access), we decided to restrict the webview access to custom made pages.

For example, we had a news sections which opened webs on the internet. What we ended up doing was open custom-made web pages which contained the title, body and an image related to the news (which still adds to resource memory usage, but we managed to keep it in a contained small format which would need thousands of news to be opened to be of any danger). I know, not ideal.

I'd still love to see some constructive answers to this question, whether it's a solution, a workaround, or any idea that can help future users with this same issue have clues on how to deal with it.



Related Topics



Leave a reply



Submit