6

When application enters in background running state, how much dirty memory usages is good to go. In apple video it's mentioned that the dirty memory should be reduced as much as we can.

But in my App, I am using navigation controller to push and pop views. After moving from about 20 different pages, the dirty memory usages reaches 30 MB or so.

Also on "dismissModalViewControllerAnimated" and "popViewControllerAnimated", dealloc is not called.

I have two doubts:

  1. With how much dirty memory is acceptable to go live?
  2. What is the alternate of navigation controller to support back button?

Thanks in advance.

Sunil Khedar
  • 441
  • 1
  • 3
  • 16
  • 1. Not 30MB. Older iOS devices only have 128MB ram, so your app alone would fill that for about 23%... 2. If you're really leaking 30MB, you're probably not releasing something, somewhere, it's not `UINavigationController`'s fault. – Douwe Maan Jul 06 '10 at 13:20
  • Hi Douwe, Thanks for replying. Why I doubt on UINavigationController because, I keep pushing view controllers in it. And when I pop the view contollers from navigation controller, dealloc method is not called where I am release the instance variables defined. And the memory is not getting cleared used by all these instance variables. Am I doing something wrong? Do I have to clear instance variables somewhere else and not in dealloc? – Sunil Khedar Jul 06 '10 at 14:35
  • Do you have any other references to your view controllers, elsewhere in your app? If you're popping them, but still have a reference somewhere else to the controller, it won't get GCed – blueberryfields Sep 28 '10 at 15:30

2 Answers2

4

You might still have your UIViewControllers still retained if dealloc isn't being called.

Perhaps are you setting delegates or other classes in these UIViewControllers that retained and referenced back up the tree (circular references).

A way you can debug this is to overload retain and release in your UIViewController and set a break point and log the retainCount.

Here is a magic snippet I leave running around that helps me a ton when I can't figure out why I'm still retaining something.

- (id)retain
{
    NSLog(@"retain \t%s \tretainCount: %i", __PRETTY_FUNCTION__ , [self retainCount]);
    return [super retain];
}
- (void)release
{
    NSLog(@"release \t%s \tretainCount: %i", __PRETTY_FUNCTION__ , [self retainCount]);
    [super release];
}
- (id)autorelease
{
    NSLog(@"autorelease \t%s \tretainCount: %i", __PRETTY_FUNCTION__ , [self retainCount]);
    return [super autorelease];
}

__PRETTY_FUNCTION__ is a special hidden macro in CLang that gives a pretty Objective-C function name as a char array.

Zac Bowling
  • 6,508
  • 1
  • 27
  • 26
0
  1. When an iOS starts running out of memory it tries to kill the background processes that are using the most memory. So while there's no absolute good number, minimising how much memory you use is a good idea. Leaving it at 30Mb is tantamount to guaranteeing that your app will be killed
  2. Unless you want to change your UI there is no need to use anything other that a UINavigationController to deal with your back button. I think the problem here is that if dealloc is not called on a pop or dismiss, you have a memory leak

Almost all view controllers have data that is effectively cached and can be regenerated when the app returns to the foreground. Think of the data that you release when you get a memory warning when the app is running. (You are responding to memory warnings, right?) It's that stuff that should be released when you go into the background.

Stephen Darlington
  • 51,577
  • 12
  • 107
  • 152