Memory not being released after deleting vc with UIWebView but no leak detected by analysis tools

StackOverflow https://stackoverflow.com/questions/23663769

  •  22-07-2023
  •  | 
  •  

Question

I've got a simple app which is just loading a view controller which contains a UIWebView. While running it I noticed that the amount of memory reported by XCode increases from 2 MB before the vc is allocated up to 12.5Mb after its been created. But then after the vc is deleted the amount of memory doesn't go down, in fact it rises even further to 13Mb. However XCode's leaks analyser doesn't report any problems.

The view controller is created and destroyed here from within the app's main (and only other) view controller:

- (void) viewDidAppear:(BOOL)animated
{
    static BOOL presented = NO;
    if (!presented)
    {
        PresentationViewController *vc = [[PresentationViewController alloc] initWithPresentationId:@"index.html"];

        __weak typeof(vc) weakVc = vc;
        [vc setExitBlock:^{
            [[weakVc presentingViewController] dismissViewControllerAnimated:YES completion:nil];
        }];

        [self presentViewController:vc animated:YES completion:nil];
        presented = YES;
    }
}

And the view controller's dealloc is called so it is being destroyed, and yes the web view delegate is being nilled.

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self.webView stopLoading];
    self.webView.delegate = nil;
}

Why isn't the memory consumption going down when the view controller is deleted?

Was it helpful?

Solution

  1. Try repeat the action over 3 times or even more , see if the memory is still keep growing or stay near a fixed value. iOS some times caches the view for further use, even if you removed it.
  2. Check your project Scheme to see if you have set the "Enable Zombie Objects" on, that could leads to crazy memory growth.
  3. try use intrumemnts, set the Allocations Track Display Style to Active Allocation Dstribution, Mark Generation->load the webveiew->quit the webview->Mark Generation->repeat 3 times. By this way you can find what's left in your memory Allocations and which codes caused it.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top