Failed to fully recover memory usage from UIWebView

I have the following code sample (using ARC) that adds a UIWebView as a subquery and subsequently deletes it (switches by clicking 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 an application initially loads with an empty UIView , it consumes approximately 1.29 MB (live bytes counted from Instru.app). Pressing the space bar of the UIView triggers the toggleWebViewLoading: function, which in turn creates a new UIWebView , loads Google and adds it as a preview. Once this sequence of operations is completed, the memory usage is approximately 3.61 MB . Pressing it again performs the second part of toggleWebViewLoading: which removes the UIWebView from its supervisor and sets it to nil . At this point, memory consumption dropped to 3.38 MB .

My question is, how can I completely restore memory from UIWebView (i.e. return the memory to its original value of 1.29 MB or something similar after uninstalling the UIWebView and set to nil )?

Other relevant information

Before anyone asks why I care so much about saving memory, I have a much more complicated situation using Xamarin / MonoTouch, where 10+ UIWebView consumes 200 MB+ memory, and I can never seem to return all the memory when it no longer needed. I think the answer comes down to this simple question.

+3
source share
1 answer

I would suggest controlling how many threads you have. UIWebView creates several threads for itself. I expect that they will not be cleaned up properly once the UIWebView is released, but this is just a hunch.

0
source

All Articles