简体   繁体   中英

ARC Retain cycle for dummies

i'm facing a memory problem with my app,

I have a nav based app, if i push, pop and push all of my controllers the app will close due to memory overload.

I think it is what we call a retain cycle :

I have a custom nav controller : MyNavController , this Controller is my root controller, and push the main UIViewController : MyMainController , when the app starts the app use about 130 MB of memory, when i push a new controller : the memory goes up to 160 then i pop this controller : the memory is still 160 (159 exactly) then i push another view controller : the memory goes up to 190 MB ... The memory never goes down.

  • Can you confirm me that is a retain cycle ?

  • If i'm not wrong when i pop a view controller the memory should decrease of the view controller memory size ?

  • I always use strong into my properties (button, view, customView, customObject...) but when i set a breakpoint into my second level controller into the dealloc method i know that it is called, so the controller should be released ?

  • I have try something : i made an empty UIViewController and set the view in my xib to one of my non released controller so it could be more heavy than clear, so this controller have no line of code, simply a .h and .m with no custom code nothing, when i push this controller the memory goes up and when i pop it the memory does not go down ! I really do not understand what i have to look for, do i have to llok for on my MainViewController ? or in the controller i push on the stack ?

I simply load my controller using :

GeoControllerViewController *aGeoController = [[GeoControllerViewController alloc] initWithNibName:@"GeoControllerViewController" bundle:nil];
aGeoController.dictionnaryModele = _dicCours;
[self.navigationController pushViewController:aGeoController animated:YES];

Thanks in advance.

You ask:

Can you confirm me that is a retain cycle?

No. Could be a leak. Could be caching. Could be a retain cycle. We can't tell from what you've shared with us.

If i'm not wrong when i pop a view controller the memory should decrease of the view controller memory size?

Generally it should decrease when you pop, but if using a cache for anything, or if populating a shared model or what have you, it might not return entirely to memory levels prior to the initial push. Having said that, if there are caches in play, if you push and pop a number of times, the total "live bytes" that you see in allocations should return to a consistent level after you push and pop a couple times.

I always use strong into my properties (button, view, customView, customObject...) but when i set a breakpoint into my second level controller into the dealloc method i know that it is called, so the controller should be released?

If dealloc of that controller is getting called, that tells you that this is not involved in any retain cycle and that the memory associated with the controller will be recovered by the system. Theoretically, any of its strong properties should get released, too.

As an aside, usually your IBOutlet references (ie those things created by the NIB/storyboard) should be weak in an ARC project.


A couple of concrete bits of advice:

  1. Run your code through the static analyzer ("Analyze" on the Xcode "Product" menu) and make sure you have zero warnings there. If you have any, fix them first.

  2. Run your code through the Leaks tool in Instruments and see if it reports anything. If it does, that will tell you how to proceed.

  3. If you still can't find the issue, run the tool using the Allocations tool in Instruments, mark a heapshot/generation before going to the next scene, go to the next scene, return back, and mark another heapshot/generation. You can then analyze what was allocated and not released between those two moments in time, which will tell you what to then look for.

    See WWDC 2012 video iOS App Performance: Memory for some demonstrations on how to do this.

Frankly, 130MB seems to be highly too much for a start in general.

  • I wouldn't be so sure about that. The question is: do you hold any references to the controllers besides the ones in navigation controller? It may be a leak as well. Why these controllers consume so much memory?

  • Yes, memory consumption should be lower.

  • I don't know if I get your question right. Usually strong properties are ok, but you have to be careful eg when two objects hold each other on strong. dealloc is called in ARC as well, so you can log out the moment when object is released.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM