Reputation: 11
Got the weirdest thing happening ... I have a pretty complex UIViewController subclass of which I'm loading through a NIB file. I push the view by simply doing the following:
SecondViewController2 *secV = [[SecondViewController2 alloc] initWithNibName:@"SecondViewController2" bundle:nil];
self.secondViewController2 = secV;
[secV release];
[self.navigationController pushViewController:secondViewController2 animated:YES];
[secondViewController2 release];
secondViewController2 = nil;
As you can see I release secondViewController2.
The thing is, when I perform it the first time, ALTHOUGH dealloc method is called in secondViewController2, I still observe in instrument an additional 2MB which do not seem to get released. There are no leaks of any sort, I checked already.
When I perform the below action the the second, third, fourth time etc. the dealloc is getting called and I DO NOT OBSERVE ANY ADDITION in memory. That alone convinces me that I do not perform the error in my code since the same code is being run the first, second third time but only the first time around 2MB are not being released. As I stated dealloc for secondViewController2 is still being run!!!
Any idea? is it bug in Apple's mechanism ?
Upvotes: 1
Views: 245
Reputation: 69027
First off, it is not possible to answer thoroughly your question without analyzing your complete implementation. But, generally speaking, if I understand correctly your question, I would say that the situation you are describing is a correct one.
You should think that there are actions that definitely have a permanent effect on memory occupation.
E.g., if you load some images into memory through [UIImage imageNamed]`, then your image will also go into an image cache which will not be emptied when the class that loaded the image is deallocated. Another good example is that of a singleton: a singleton class occupies some memory the first time it is instantiated and it usually never releases it (before the program ends - and when I say singleton, read also any kind of static globals).
Those are just 2 examples, but there could be many more cases of this. And it could be done in your own code, but it could also be done by some framework that your controller uses. This effect would be more likely if your controller is the first controller you instantiate in you app that uses some kind of functionality. But basically, it cannot be known before hand, I would say, but only after inspection of your program behavior via Instruments.
The really valuable information for you is that upon successive instantiation/deallocation of you controller class, the memory occupation does not grow. Generally speaking, this is a sign of two things:
your class has no memory leaks;
your class has not abandoned memory.
What 1+2 implies is that the overall memory consumption of your program (as far as your controller is concerned) is flat. (although it has a cost that you pay on first use).
So, in my view, your issue is at most one of "optimization" of memory usage by your controller. As I said, one should know exactly what your controller does to know whether those 2MB comes from some cache, or whatever else (and in some cases it could be not under your control either).
Hope it helps.
Upvotes: 2