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?
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?
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.
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.
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.
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.