10

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:

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.

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.

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.

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];

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];

I also do:

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

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?

Thanks.

Community
  • 1
  • 1
Burjua
  • 12,506
  • 27
  • 80
  • 111
  • it seems like very old and well known problem, people are running into it since 2008 and still no solution(((( http://discussions.info.apple.com/thread.jspa?threadID=1729697 – Burjua Aug 27 '10 at 10:28
  • Ok, I found one idea how to work around this issue, it's described here:http://www.iphonedevsdk.com/forum/iphone-sdk-development/34840-uiwebview-memory-crash-caching-memory-useage.html . It't not a solution to a problem, but some kind of hack. I can't beleive that it's the only way to do it, any other thoughts? Thanks – Burjua Aug 27 '10 at 11:44
  • possible duplicate of [Does UIWebView leak memory?](http://stackoverflow.com/questions/648396/does-uiwebview-leak-memory) – Brad Larson Aug 27 '10 at 15:20
  • See also [is it possible to free memory of UIWebView?](http://stackoverflow.com/questions/2184688/is-it-possible-to-free-memory-of-uiwebview) – Brad Larson Aug 27 '10 at 15:22
  • Ok, thanks, but there is no solution there( – Burjua Aug 31 '10 at 09:46

3 Answers3

3

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];

I think then ur problem is solved

Mark Coleman
  • 40,542
  • 9
  • 81
  • 101
Naveen Shan
  • 9,192
  • 3
  • 29
  • 43
0

I know this thread is a bit old but just for an idea and to add few more relevant bits i would like to add these tips so the future visitors can have a look at this as well and there problem might get solved.

  • You should set Cache policy to your NSURLRequest object and check for policies that ignore cache data, see the documentation.

  • Since you have a call to release in your code i assume you are using ARC, so why you do not call [super dealloc] in viewDidUnload()?

  • Have a look at this thread for how to cleanForDealloc using UIWebView.

  • Is it a UITableViewController ? with a custom cells ? Try deallocating (release, removeFromSuperView, set to nil) them as well.

Hope it helps!

NeverHopeless
  • 11,077
  • 4
  • 35
  • 56
0

I guess that since you set webview=nil, you lose any chance to release it.

xpda
  • 15,585
  • 8
  • 51
  • 82
VdesmedT
  • 9,037
  • 3
  • 34
  • 50