3

I have the following sample code (using ARC) which adds a UIWebView as a subview and subsequently removes it (toggled by a tap gesture on the screen):

- (void)toggleWebViewLoading:(UITapGestureRecognizer *)sender
{
    if (webView == nil)
    {
        webView = [[UIWebView alloc] initWithFrame:CGRectMake(0.0f, 100.0f, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 100.0f)];
        [webView loadRequest:[[NSURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:@"http://www.google.ca"]]];
        [self.view addSubview:webView];
    }
    else
    {
        [webView removeFromSuperview];
        webView = nil;
    }
}

When the app initially loads with a blank UIView, it consumes approximately 1.29 MB (live bytes reading from Instruments.app). Tapping on the blank UIView causes the toggleWebViewLoading: function to fire which in turn creates a new UIWebView, loads Google, and adds it as a subview. Once this sequence of operations have been completed, the memory usage is approximately 3.61 MB. Tapping again executes the second part of toggleWebViewLoading: which removes the UIWebView from its superview and sets it to nil. At this point the memory consumption has dropped to 3.38 MB.

My question is, how can I completely reclaim the memory from UIWebView (i.e. have the memory consumption return to its initial value of 1.29 MB or something similar after the the UIWebView has been removed and set to nil)?

Other Relevant Information

Before someone asks why I care so much about ~2 MB of memory savings, I have I much more complicated situation using Xamarin/MonoTouch where 10+ UIWebView's are consuming 200 MB+ of memory and I can't ever seem to reclaim all of the memory when it is no longer needed. I think the answer boils down to this simple question.

safwanc
  • 3,351
  • 2
  • 15
  • 20
  • Try issuing a memory warning after the webview is released and se if that reduces your memory. I don't think it's always correct to just look at the numbers since stuff easily can be cached by some framework code in waiting for better things to do with the ram. – Peter Segerblom Oct 17 '13 at 20:03
  • UIWebView is known to be quite a "heavy" object. Here is an interesting read I found – the guy faces quite the same symptoms: http://blog.techno-barje.fr//post/2010/10/04/UIWebView-secrets-part2-leaks-on-release/ – ivanmoskalev Oct 17 '13 at 20:10
  • @user2404543 I tried adding `[self didReceiveMemoryWarning]` after the UIWebView is set to nil but it had no impact on the memory usage. – safwanc Oct 17 '13 at 20:55
  • @ivanmoskalev Thanks for the link, thats pretty much exactly the same problem I'm facing. No particular hack seems to help and I even verified that "Enable Zombie Objects" is unchecked (as mentioned in one of the comments). – safwanc Oct 17 '13 at 20:56
  • possible duplicate of [MonoTouch Memory Consumption Overhead for UIWebView](http://stackoverflow.com/questions/19436946/monotouch-memory-consumption-overhead-for-uiwebview) – Krumelur Oct 17 '13 at 21:02
  • @safwanc for future reference you issue a memory warning on the simulator through the menu hardware->simulate memory warning. I duplicated your code and ran it first memory warning reduced memory usage by about 50% from 20mb to 10mb second memory warning reduced it slightly more to about 8. – Peter Segerblom Oct 18 '13 at 08:12
  • @Krumelur I don't think its a possible duplicate because this question was focusing on why the memory can never be completely reclaimed from a UIWebView. The other question you linked to is comparing the memory consumption overhead of UIWebView in Objective-C vs. MonoTouch. – safwanc Oct 18 '13 at 14:21
  • @user2404543 Hmm, how did your memory consumption go up to 20MB in the first place? I just tried issuing the "Simulate Memory Warning" through the hardware menu and it barely affected "Live Bytes" reading in Instruments. – safwanc Oct 18 '13 at 14:27
  • @safwanc no i did not run it in profiler i just watched the memory consumption in the left menu under debugger "tab" xcode5. I just assumed they were the same. What could it be if not something like currently allocated memory? – Peter Segerblom Oct 18 '13 at 19:30
  • See the similar answers from [stack overflow][1] [1]: http://stackoverflow.com/questions/2933315/clear-uiwebview-content-upon-dismissal-of-modal-view-iphone-os-3-0 – iPC May 14 '15 at 12:38

1 Answers1

0

I would suggest monitoring how many threads you have active. UIWebView spawns several threads for itself. I expect they are not cleaned up properly once the UIWebView is deallocated, but it's just a hunch.

Holly
  • 5,270
  • 1
  • 24
  • 27