我的webView占用了太多内存吗?

时间:2014-12-10 06:54:24

标签: swift memory uiwebview

我有一个简单的示例,其中一个webView在应用程序加载成功时加载一个链接,Debug导航器如下所示:

enter image description here

应用程序占用73.5 MB的内存之后,当我向下滚动到模拟器时,内存持续增加太多,如:

enter image description here

任何人都可以解释为什么它会占用太多内存吗? 或者告诉我它是否会引起任何问题?

这是我的代码:

import UIKit

 class ViewController: UIViewController {

@IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
   super.viewDidLoad()

    let url = "https://developer.apple.com/library/mac/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-XID_456"
    let requestURL = NSURL(string:url)
    let request = NSURLRequest(URL: requestURL!)
    webView.loadRequest(request)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

   }
}

1 个答案:

答案 0 :(得分:0)

你可以使用WKWebView代替UIWebView,它不会占用更多内存,下面是一个如何在 swift 3 中使用它的例子:

    import UIKit
    import WebKit

    class ViewController: UIViewController 

{

        var webView: WKWebView!

        override func viewDidload() {
        super.viewDidload()

        let url = "https://developer.apple.com/library/mac/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-XID_456"
        let requestURL = URL(string:url)
        let request =  URLRequest(url: requestURL!)
        webView = WKWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height))
        self.view.addSubview(webView)
        webView.load(request)
}
}