iOS 12+ Memory Leak - WKWebView and UIWebView
Asked Answered
B

2

14

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

B answered 7/11, 2018 at 10:8 Comment(1)
The good news is that I've tested on iOS 13 Beta, and this issue appears to be fixed! The bad news is that Apple probably won't fix this for iOS 12 variants.B
D
3

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.

Divergency answered 19/11, 2018 at 12:25 Comment(7)
The funny thing is, you don't even need to load any content in the webview to trigger the leak. No reply from Apple yet on my bug. Will post back as soon as I hear anything. Glad you found my post :)B
Have you tried the solution posted here: #16514730 Maybe setting the URL and html to nil in viewWillDisappear might help? But, if you are saying that simply instantiating UIWebView creates the leak this shouldn't matter. One thing I did find interesting is reusing the webview doesn't introduce any new leaks, so I think it's not that big of a problem as long as you don't create many many webviews.Divergency
Yes I have. I've tried absolutely everything. The only thing that worked was going back to iOS11.X. As soon as you switch to iOS12+ again, the leaks return.B
Hmm yea it's an unfortunate leak. Really glad I found this question as I would have spent too much time doing what you have already done. Fingers crossed Apple releases a fix soon.Divergency
I'm surprised more people haven't come across it. I'm glad it's helped you, as I spent nearly a whole day chasing it down!B
Just found your post, glad I'm not the only one to have hit this. Fingers crossed Apple have a fix.Homebody
Agree that this is an Apple issue. I am creating a webview ONLY to send a javascript command to get the userAgent. No loading of HTML. This also causes the leak: [[[[UIWebView alloc] init] stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"] stringByAppendingString:@" Custom-Agent"];Chitter
C
2

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)

Conglomerate answered 26/11, 2021 at 13:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.