Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 04-22-2010, 02:23 PM   #1 (permalink)
Registered Member
 
Join Date: Feb 2010
Location: Finland
Posts: 9
jotos is on a distinguished road
Default How to free memory after UIWebView

I am showing a local html file by adding a UIWebView as a subview to my view controller. After dismissing the view the allocated memory does not get released. What am I doing wrong?

First I present a new view controller like this

-(IBAction)rulesButtonPressed {
[audio play:CLICK];
RulesView *rulesView = [[RulesView alloc] initWithNibName:@"RulesView" bundle:nil];
[self presentModalViewController:rulesView animated:YES];
[rulesView release], rulesView = nil;
}

The new view controller loads like this and the html page opens ok.

- (void)viewDidLoad {
// add a UIWebView to cover the whole view area with the page Rules.html
UIWebView *htmlRules = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 416)];
htmlRules.scalesPageToFit = YES;
htmlRules.autoresizingMask=(UIViewAutoresizingFlex ibleHeight | UIViewAutoresizingFlexibleWidth);
NSString *webPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/Rules.html"];
NSURL *rulesURL = [[NSURL alloc] initFileURLWithPath:webPath];
NSURLRequest *requestObj = [[NSURLRequest alloc] initWithURL:rulesURL];
[htmlRules loadRequest:requestObj];
[self.view addSubview:htmlRules];
[rulesURL release];
[requestObj release];
[htmlRules release];
[super viewDidLoad];
}

Then I dismiss the new view controller

-(IBAction) backButtonPressed {
[self dismissModalViewControllerAnimated:TRUE];
}

This works otherwise fine but the problem is that memory usage goes up about 6 MB when the web view is loaded for the first time but only goes down by about 1.5 MB when the view controller is dismissed. Where does that 4.5 MB go to? If I continue toggling the view on and off the memory usage change between present and dismiss keeps to that 1.5 MB i.e. it is not leaking. But how do I release that 4.5 MB when I'm done with the web view?
jotos is offline   Reply With Quote
Old 04-22-2010, 05:06 PM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

If you force a memory warning, does it go away?
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 04-22-2010, 11:00 PM   #3 (permalink)
Registered Member
 
Join Date: Feb 2010
Location: Finland
Posts: 9
jotos is on a distinguished road
Default

No, a simulated memory warning does not change the situation.
jotos is offline   Reply With Quote
Old 04-23-2010, 08:31 AM   #4 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Hrm. Well, if you're positive it has nothing to do with your code - and nothing is jumping out at me here - then I don't think there's much you can do. I was wondering if the web view was keeping some kind of cache, and that maybe the memory warning would force it to dump.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 04-23-2010, 10:41 AM   #5 (permalink)
Registered Member
 
Join Date: Feb 2010
Location: Finland
Posts: 9
jotos is on a distinguished road
Default

I tested it several different ways; with/without the nib file initialization, with/without delegate, addSubView/removeFromSuperView instead of present/dismiss etc.. but nothing seems to help. It really behaves as if there was a cache that gets loaded the first time I load the file. After all there is no leak since the max usage does not go up no matter how many times I open the view.
But if this is a web view feature and not my bug it is really a strange feature. That cache itself could increase the probability of memory warnings in a crowded system? But I think I can live with it if you don't see any obvious error in my code. Thanks.
jotos is offline   Reply With Quote
Old 04-23-2010, 10:45 AM   #6 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

On the assumption that this is in the modal view controller, see if this change makes any difference:

Code:
-(IBAction) backButtonPressed
{
   [[self parentViewController] dismissModalViewControllerAnimated:TRUE];
}
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 04-23-2010, 11:17 AM   #7 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 580
ChrisL is on a distinguished road
Default

I've seen this issue at my previous job and posted about it in a prior thread. We found that each web view we allocated was spawning a new thread that was never being properly cleaned up, even after the web view was destroyed. In our case, we were creating and destroying multiple web views as the user navigated between the various view controllers, and we found that each one was leaking additional memory until the app would eventually crash. We ended up hacking around this by making a single web view object that was persistent for the entire lifespan of the app. We never found a way to reclaim all of the memory used, but for that particular app it luckily wasn't a problem.

We never were able to find a fix, and I believe one of the devs filed a bug report with Apple. I haven't followed up on it since then (this was back when 3.0 was just released), so I don't know what the current state of things is. At the very least, I hope that creating and destroying multiple web views no longer leaks additional memory each time, but it sounds like the problem still exists in at least some form. I'll have to make a note to look into this more thoroughly -- I haven't needed to use web views much in my recent projects, but it would be good to stay on top of this.
ChrisL is offline   Reply With Quote
Old 04-24-2010, 06:39 AM   #8 (permalink)
Registered Member
 
Join Date: Feb 2010
Location: Finland
Posts: 9
jotos is on a distinguished road
Default

I made some further tests and I think I found the key to the feature. The behavior changes if I uncheck "Detects Links" in the nib file. If that is checked it allocates in my case about 5 MB of memory immediately when the page is opened and the allocation stays constant. But if the radio button is unchecked the memory gets allocated dynamically i.e. not much when the page is opened but the usage starts to increase if the page is scrolled. Eventually, scrolling the page from the beginning to the end results in the whole 5 MB getting allocated again.

Now, the main difference is that if that Detects Links is not checked the whole allocated memory gets freed when I exit the view! And that solves my memory issue since I don't need the links detecting feature.
jotos is offline   Reply With Quote
Old 04-24-2010, 09:35 AM   #9 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 580
ChrisL is on a distinguished road
Default

Wow, great find. You should definitely file a bug report with Apple about this; I doubt that this is the intended behavior.
ChrisL is offline   Reply With Quote
Reply

Bookmarks

Tags
uiwebview

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 312
20 members and 292 guests
baja_yu, cgokey, Domele, Duncan C, Fstuff, gbenna, givensur, guusleijsten, HowEver, iphonedevshani, jbro, JoeRCruso, mdpauley, n00b, newDev, seokwon lee, SLIC, stanny, Steven.C, WheyLabs
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,648
Threads: 94,112
Posts: 402,875
Top Poster: BrianSlick (7,990)
Welcome to our newest member, brandon6031
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 07:33 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0