Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone: Reusing UIViewControllers in order to save memory

what are best pratices to reuse UIViewControllers? In many apps (including Apple's own examples: e.g. SQLiteBooks), UIViewControllers are allocated and initialized everytime, a UIViewController is pushed to the stack. This increases the use of memory with every new controller, because the objects stay in memory and aren't used again.

How to make it better?

like image 348
Stefan Avatar asked Nov 17 '25 00:11

Stefan


2 Answers

This increases the use of memory with every new controller, because the objects stays in the memory and aren't used again.

It should be released when the stack is popped though, as long as you have not got something else holding on to it. Check your dealloc methods are getting called.

Also if it is pushed to the stack, then you need to keep it around at least until it is popped (which automatically happens if you follow the standard patterns). So it is used again.

So following the standard pattern should already keep your memory usage as small as you can get away with.

like image 196
frankodwyer Avatar answered Nov 19 '25 16:11

frankodwyer


This is what I do when creating a new viewcontroller and the memory is released when the view is removed from the window

MyViewController *mvc = [[[MyViewController alloc] initWithNibName:@"MyView" bundle:nil] autorelease];
[[self navigationController] pushViewController:mvc animated:YES];
like image 30
lostInTransit Avatar answered Nov 19 '25 15:11

lostInTransit