Does UIWebView leak memory?

2019-01-11 11:33发布

问题:

If your iPhone app uses a UIWebView, and loads many pages with large images, you may find that you are leaking memory. Can this be resolved?

回答1:

Try adding

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

right before you release the webview. For a leak in 4.2.1 relating to displaying a PDF in a UIWebView this solves most of the leak problems for me.



回答2:

It is a known problem.

Marco Arment mentions the leak here. http://blog.instapaper.com/post/60628543

There isn't a lot you can do. One option in to "intercept" the html try to truncate parts that you don't need. Obviously, this won't likely work for a general purpose webview.

In the meantime, you may just have to wait for an update from Apple.



回答3:

I got a

received memory warning, level 1

and then the application crashed with the huge text size in the web view.

This article helped me, solving the leak issue. Very easy and straightforward to use.



回答4:

I did this and it seems to help (looking at Leaks and running through simulator anyway):

MyWebViewController *myWebViewController = [[[MyWebViewController alloc] initWithNibName:@"MyWebView" bundle:[NSBundle mainBundle]] autorelease];

It was the "autorelease" that seems to help in that the leak is not getting bigger on each webview I open.