WKWebView memory issue causes crash

I have a simple app with a WKWebView. When running on iOS 9.3, the memory usage stays around 5 Mb with is expected since WKWebView runs in a separate process. But memory usage for "other processes" goes high after a couple of navigations, especially on heavy pages. When the memory usage reaches its threshold, the app gets two memory warnings and then gets terminated. The debugger gives no output. I find this behavior unexpected since one of the features with WKWebView is that the app won't terminate when WKWebView runs out of memory. It should instead kill the WKWebView process and trigger webViewWebContentProcessDidTerminate. But

webViewWebContentProcessDidTerminate is
never called.


This is my code:

import UIKit
import WebKit


class MyAwsomeWebViewController: UIViewController, WKNavigationDelegate {
    
    var page:WKWebView! = WKWebView()


    @IBOutlet weak var WKBaseView: UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        page.navigationDelegate = self
        if let url = URL(string: "https://hitta.se") {
            let request = URLRequest(url: url)
            page.load(request)
        }


    }
    
    override func loadView() {
        self.view = page
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        print("**** MEMORY WARNING! ****")
    }
    
    func webViewWebContentProcessDidTerminate(_ webView: WKWebView) {
        print("*** Terminate WebView Process ***")
    }
}


Why does my app get terminated and how can I prevent this?

WKWebView is not a free pass to use however much memory you want with web content - Its memory usage is attributed to your app.

Consider it the same as if you used (the deprecated) UIWebView and all of that web page memory usage *was* actually in your process space. The same web pages and navigations you're doing here could cause the same problem with UIWebView.

It is unexpected that just a couple of navigations triggers a memory pressure warning. But with the exact URLs you see this with, more debugging could be done.
WKWebView memory issue causes crash
 
 
Q