views:

28

answers:

1

I feel like I don't understand something fundamental here. I've been working on memory management in my app while using Instruments to check out live allocations. I have a modal view controller (settingsViewController) that has an image for a background. One thing I noticed was that even after settingsViewController dealloc is called, there still is a live Malloc 520 KB, ImageIO is the responsible library. I'd expect live memory to drop back down after dismissing settingsViewController. Is the UIImageView still hanging around somewhere?

Here is how I load the image in viewDidLoad, as well as dismiss the view controller when I'm finished.

- (void)loadView {
    [super loadView];

    ////// background ////////
    UIImageView *background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"settings_background.png"]];
    [self.view addSubview:background];
    [background release];
    //////////////////////////
}

- (void)viewDidLoad {
    [super viewDidLoad];

    ///////// done button //////////
    UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed)];
    self.navigationItem.leftBarButtonItem = done;
    [done release];
    ////////////////////////////////
}

- (void) doneButtonPressed {
    [self dismissModalViewControllerAnimated:YES];
}

- (void)dealloc {
    NSLog(@"settingsViewController dealloc"];
    [super dealloc];
}

At this point, this is all that is in the settingsViewController, so no need to do anything special in dealloc, right? Here is how I'm showing testViewController in the rootViewController.

- (void) loadSettingsView {
    SettingsViewController *settingsViewController = [[SettingsViewController alloc] init];
    UINavigationController *settingsNavigationController = [[UINavigationController alloc] initWithRootViewController:settingsViewController];
    [self presentModalViewController:settingsNavigationController animated:YES];
    [settingsViewController release];
    [settingsNavigationController release]; 
}

I'd like to make sure I understand what is going on before moving forward. I have several different modal view controllers, each with a different image as a background. Since each one creates a Malloc 520 KB, I end up using 2-3 MB of precious memory for no good reason. What is holding on to that 520 KB?

+2  A: 

When you use the +[UIImage imageNamed:] method, the framework caches the image data for you. That's why you see it hold onto some memory even after your view is released. If you're working in the simulator and you want to see it release that memory, send the simulator a memory warning after you've dismissed your view. The image framework should then release the cached image data.

Jason Coco