UIWebView taking lots of memory

2019-02-06 11:07发布

In my app when i load UIWebView with any Website url the memory jumps from 30mb to around 140mb. I am using ARC

here is the image for the same
and when dismissing the UIWebViewController[Viewcontroller which contains UIWebView], it doesnt releases the memory. Can any body help me how to solve this memory issues as well as please also provide me pointers of memory bestpractices in ARC

For loading the webpage :-

NSURL *nsurl=[NSURL URLWithString:self.url];
    NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
    [webview loadRequest:nsrequest];

FOr Dismissing the Viewcontroller:-

[self dismissViewControllerAnimated:YES completion:^{
    webview.delegate=nil;
    webview=nil;
}];

Thanks in Advance :)

9条回答
冷血范
2楼-- · 2019-02-06 11:51

I was having the same problem in my application. I would recommend that you should use WKWebView. WKWebView was introduced with iOS 8. You can read more about it here apple documentation

The memory issue is definitely solved. You will get all the functionality from webview and some more(i.e. progress). Hope this helps

查看更多
甜甜的少女心
3楼-- · 2019-02-06 11:52

Don't listen to them,

Its not a problem, phones can handle all that memory usage, try using an MKMapView and watch your memory usage soar.

In your simulator, try putting through a low memory warning cmd+shift+m, and see what happens, you can't expect all web views to run at the same memory usage, also, you have give a chance for your phone to buffer it all out.

iPhones can handle all of that, so you shouldn't worry much about it.

But try this anyway:

The behaviour of UIWebViews changes if you uncheck "Detects Links" in the nib file, so try toggling that and see what happens.

查看更多
在下西门庆
4楼-- · 2019-02-06 11:52

If you want to reduce the memory usage of UIWerview immediately, you can try:

1.

[webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];

or

2.

[webview loadHTMLString: @"" baseURL: nil];

or

3.

[webview loadData:nil MIMEType:nil textEncodingName:nil baseURL:nil];

It worked to me.

查看更多
登录 后发表回答