我目前正在 iOS 5.1 中构建一个使用 ARC 的导航控制器应用程序。我经常需要显示网页,我制作了一个 Web 查看器,它只是一个 UIWebView,两侧有一些自定义内容。当用户看完页面后,他们点击后退按钮,这将释放与自定义 Web 查看器相关的所有内存。我的问题是,当按下后退按钮时,所有内存似乎都没有被释放。我构建了一个玩具应用程序(在 github 上 https://github.com/bfruin/WebViewMemory)这只是几个按钮,每个按钮都有一个调用不同页面的第一响应者。
@implementation ViewController
-(IBAction)googlePressed:(id)sender
{
CustomWebView *customWebView = [[CustomWebView alloc] initWithStringURL:@"http://www.google.com"];
[self.navigationController pushViewController:customWebView animated:NO];
}
-(IBAction)ksbwPressed:(id)sender
{
CustomWebView *customWebView = [[CustomWebView alloc] initWithStringURL:@"http://www.ksbw.com/news/money/Yahoo-laying-off-2-000-workers-in-latest-purge/-/1850/10207484/-/oeyufvz/-/index.html"];
[self.navigationController pushViewController:customWebView animated:NO];
}
-(IBAction)feedProxyPressed:(id)sender
{
CustomWebView *customWebView = [[CustomWebView alloc] initWithStringURL:@"http://feedproxy.google.com/~r/spaceheadlines/~3/kbL0jv9rbsg/15159-dallas-tornadoes-satellite-image.html"];
[self.navigationController pushViewController:customWebView animated:NO];
}
-(IBAction)cnnPressed:(id)sender
{
CustomWebView *customWebView = [[CustomWebView alloc] initWithStringURL:@"http://www.cnn.com/2012/04/04/us/california-shooting/index.html?eref=rss_mostpopular"];
[self.navigationController pushViewController:customWebView animated:NO];
}
CustomWebView 只是 IB 中链接到 UIWebView 属性的 UIWebView
@implementation CustomWebView
@synthesize webView, link;
- (id)initWithStringURL:(NSString *) stringURL
{
if (self = [super init]) {
link = stringURL;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:link];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
我的问题是,一旦加载了所有内容,我就将堆基线设置为初始 ViewController。然后,我在加载页面后检查堆,然后返回 ViewController 并获得以下堆快照:
这表明,在每次单击按钮并返回 ViewController 后,即使 CustomWebView 都应该被释放,堆也会继续增长。
EDIT:
上述示例项目可以找到在 github 上 https://github.com/bfruin/WebViewMemory
我有同样的问题,我发现这个小魔法 http://www.codercowboy.com/code-uiwebview-memory-leak-prevention/
/*
There are several theories and rumors about UIWebView memory leaks, and how
to properly handle cleaning a UIWebView instance up before deallocation. This
method implements several of those recommendations.
#1: Various developers believe UIWebView may not properly throw away child
objects & views without forcing the UIWebView to load empty content before
dealloc.
Source: http://stackoverflow.com/questions/648396/does-uiwebview-leak-memory
*/
[webView loadHTMLString:@"" baseURL:nil];
/*
#2: Others claim that UIWebView's will leak if they are loading content
during dealloc.
Source: http://stackoverflow.com/questions/6124020/uiwebview-leaking
*/
[webView stopLoading];
/*
#3: Apple recommends setting the delegate to nil before deallocation:
"Important: Before releasing an instance of UIWebView for which you have set
a delegate, you must first set the UIWebView delegate property to nil before
disposing of the UIWebView instance. This can be done, for example, in the
dealloc method where you dispose of the UIWebView."
Source: UIWebViewDelegate class reference
*/
[webView setDelegate:nil];
/*
#4: If you're creating multiple child views for any given view, and you're
trying to deallocate an old child, that child is pointed to by the parent
view, and won't actually deallocate until that parent view dissapears. This
call below ensures that you are not creating many child views that will hang
around until the parent view is deallocated.
*/
[webView removeFromSuperview];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)