uiwebview 和巨大的内存丢失 [英] uiwebview and huge memory loss

查看:17
本文介绍了uiwebview 和巨大的内存丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 UIWebViews 时遇到问题,我在这里看到了同样的问题,但没有有用的答案.问题在这里: UIWebView 内存管理 .我会引用它:

I have a problem using UIWebViews, I've seen the same question here but there wasn't helpful answer. the question is here: UIWebView memory management . I will quote it:

我正在开发一个大量使用 UIWebView 的应用程序.这个应用程序在从我的服务器加载内容时动态生成大量 UIWebView.其中一些 UIWebView 非常大,并且有很多图片.

I am developing an application that makes heavy use of UIWebView. This app generates dynamically lots of UIWebViews while loading content from my server. Some of these UIWebViews are quite large and have a lot of pictures.

如果我使用仪器检测泄漏,我不会检测到任何泄漏.但是,分配了很多对象,我怀疑这与 UIWebViews 有关.

If I use instruments to detect leaks, I do not detect any. However, lots of objects are allocated and I suspect that has to do with the UIWebViews.

当 webviews 因为不再需要而释放时,似乎并不是所有的内存都被释放了.我的意思是,在向我的服务器发出请求后,应用程序会创建一个 UITableView 和许多 webviews(仪器说大约 8Mb).当用户点击返回时,所有这些都被释放,但内存使用量仅减少约 2-3 Mb,并在使用应用程序 5-10 分钟后崩溃.

When the webviews release because no longer needed, it appears that not all memory is released. I mean, after a request to my server the app creates an UITableView and many webviews (instruments say about 8Mb). When user tap back, all of them are released but memory usage only decrements about 2-3 Mb, and after 5-10 minutes using the app it crashes.

我创建了简单的测试应用程序并得到了相同的结果.

I have created simple test app and have the same results.

这是一个tableView,我正在创建这样的DetailsView:

It's a tableView, I'm creating DetailsView like this:

DetailsVC *detailViewController = [[DetailsVC alloc] initWithNibName:@"DetailsVC" bundle:nil];
detailViewController.n = indexPath.row;
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];

在 DetailsVC 中,我在 IB 中创建了一个 webView.我像这样加载html:

in DetailsVC I have a webView created in IB. I load html like this:

    NSString *urlAddress;
if (self.n == 0)
{
    urlAddress = @"http://www.google.com";
}
else 
{
    urlAddress = @"http://www.yahoo.com";
}

NSURL *url = [NSURL URLWithString:urlAddress];

NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

[self.webView loadRequest:requestObj];

我也这样做:

- (void)viewDidUnload {
self.webView = nil;
}

就是这样,每次我在 RootViewController 中选择任何 webView 都会丢失 2-3 Mb 的内存,有没有解决这个问题的方法?

That's it, every time I choose any webView in RootViewController I'm loosing 2-3 Mb of memory, Is there a solution to this problem?

谢谢.

推荐答案

只需检查以下1.你是否将webview作为属性删除它2. 并将以下代码放入Appdelegate

Just check the following 1. Is u make the webview as property remove it 2. And put the following code in didFinishLaunchingWithOptions in Appdelegate

    NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
    [NSURLCache setSharedURLCache:sharedCache];
    [sharedCache release];

我想你的问题解决了

这篇关于uiwebview 和巨大的内存丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆