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:
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;
webViewDidFinishLoading
seems not reliable: it is executed beforedocument.readyState
becomescomplete
;document.documentElement.height
, the official way of retrieving the page height seems not reliable, too: workaround is getting the "computed style" of thebody
part and read itsheight
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
Locking a Uisearchbar to the Top of a Uitableview Like Game Center
Face Recognition on the Iphone
Development Team Not Showing in Xcode
Uiview Touch Event in Controller
Pods-Resources.Sh Permission Denied in iOS Project
Presenting Viewcontroller with Navigationviewcontroller Swift
Linking Child View Controllers to a Parent View Controller Within Storyboard
How to Develop iPhone Mdm Server
Arkit Hide Objects Behind Walls
How to Add a Button to Uinavigationbar
Dial a Phone Number with an Access Code Programmatically in iOS
Uicollectionview Insert Cells Above Maintaining Position (Like Messages.App)
Why Is There an Frame Rectangle and an Bounds Rectangle in an Uiview
iOS Builds/Ipa Creation No Longer Works from the Command Line
Coremotion Updates in Background State
Why Is My iOS App Not Showing Up in Other Apps' "Open In" Dialog