CPR
CPR

Reputation: 773

iOS 12+ Memory Leak - WKWebView and UIWebView

This is not a question, but I'm posting here in the hope that it will save someone else's sanity (I've just spent the best part of a day figuring this one out). I've identified a memory leak that has appeared from iOS 12.0+, which affects WKWebView and UIWebView. The leak appears as soon as you instantiate either of the 2 webviews. Instruments identifies the leak as coming from JavaScriptCore, which I guess is why it affects both webviews equally.

I was trying to figure out what I was doing wrong, when I decided to try an older iOS version (11.4 - the version before 12.0), and I noticed the leak had disappeared. I can reproduce this every time.

To reproduce, all you need to do is allocate an instance of a webview on a class.

let webview = WKWebView()

or

let webview = UIWebView()

Immediately, you'll notice 4 leaks on iOS 12.0/12.1, which are no longer present if you run the code on iOS 11.4. There are 4 separate leaks; 3 x 96 bytes and 1 x 128 bytes.

Leak-WKWebView

I've filed a bug with Apple via the BugReporter, duplicated at Openradar: https://openradar.appspot.com/radar?id=6132657108811776

Upvotes: 14

Views: 4194

Answers (2)

danesz
danesz

Reputation: 505

Putting it here just for reference (if someone has similar issues).

It seems there is an other, still active memory leak bug in all WKWebview versions with evaluateJavascript https://bugs.webkit.org/show_bug.cgi?id=215729

I have built a workaround for it by using WebSocket communication as an alternative way to send messages to the webview.

If you need it in form of a library (Swift package) https://github.com/Danesz/Sidewalk

(The package is a PoC, but it can help you overcome the issue, and fine-tune it for your own case)

Upvotes: 2

Null TX
Null TX

Reputation: 131

Same problem here. Was testing my app for any memory leaks and as soon as I open a UIWebView it will give me those exact leaks.

I thought it has to do with the javascript from the baseurl but setting it to nil yields the same result. This is my setup for the webview:

NSURL *url = [NSURL URLWithString:@"https://example.com"];
[self.webView loadHTMLString: htmlContent baseURL: url];

Hope Apple gives us a solution soon.

Upvotes: 3

Related Questions