WKWebView retains memory when closed

My app opens several tabs, each of which uses a WKWebView. As each new WKWebView is opened, the memory usage ascribed to my app remains relatively low but the "Other Processes" measurement (as shown in Xcode's runtime memory view) goes up significantly. If a sufficiently large number of WKWebViews are opened, the "Other Processes" number climbs to such a point that the total system free memory drops below 30MB or so. At that point my app closes unceremoniously; there is no console message, no crash log, nothing. At most I'll see an Xcode message about my device having been disconnected—the same one you get when you unplug your device during a debugging session. Even if I release all the WKWebViews in didReceiveMemoryWarning, "Other Processes" memory usage remains high and the app is closed abruptly after didReceiveMemoryWarning returns.


I find also that once I've opened a number of WKWebViews, if I delete them again by removing them from their superview and setting all references to nil, the "Other Processes" usage remains high. If I then open up more WKWebViews, they continue to push the total memory usage up and I get the same result. In other words, my app will crash when memory pressure gets high regardless of whether I open several WKWebViews at the same time or whether I open and close them one after another.


This has all the markings of a memory leak. It's as if, when I remove my WKWebViews they aren't actually removed; their memory sticks around.


Is this a known problem? Do others see that WKWebViews retain memory after being removed from the view hierarchy and the reference graph by the host application? Is there some way to force the WebContent process (where WKWebViews live) to release any memory it's holding onto—to garbage collect, in effet?

Got the same issure in iOS 9.2, and couldn't believe Apple hadn't solved it for months...

Did you ever figure out how to resolve this issue? I'm having a very similar problem.

Remember, unused memory is wasted memory. As long as memory is freed when receiving a memory warning (only one level now before the system pulls the kill switch) or upon going to background mode, everything should go back to normal and what you're seeing is normal.

Do you add a WKScriptMessageHandler to the WKUserContentController? If you do, you might create a retain cycle:

https://stackoverflow.com/a/26383032/354018

WKWebView retains memory when closed
 
 
Q