Reputation: 8489
I have created a very simple iPhone application, with single view and a UIButton with action to show UIAlertView. I am using following code
- (IBAction)showAlert:(id)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Message" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
Instruments allocations tool screen shot is attached.
In the image,low memory allocations shows app running without showing UIAlertView
, and high peaks shows memory usage after i have shown UIAlertView
, even i have dismissed UIAlertView
and release it but it continues to show same memory state and living objects (as high as 50,000). but when i press home button app goes in background and you can see in attached image living objects and used memory decrease.
Questions:
Whats wrong with my code?
Why even after releasing UIAlertView
it shows high memory allocation and living objects?
Why i have to go to background to release memory and living objects?
Upvotes: 1
Views: 478
Reputation: 6336
I would not worry too much about it, unless this is the major memory bottleneck in your app, or unless each UIAlertView cumulatively adds more memory.
Your code is correct. But even though you release the UIAlertView, the user interface system holds on to it at least until you dismiss the alert. In addition to the UIAlertView itself, it also allocates graphics resources needed to show the alert. After you dismiss the alert, it depends on when the autorelease loop kicks in, and on caching parameters, whether and when the memory gets released.
When you push the home button, then your application goes to the background, and as part of that memory deallocation is forced. That is, caches are emptied, autorelease cycle is run, etc.
Upvotes: 1