简体   繁体   English

ARC保留假人的循环

[英]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. 我有一个自定义导航控制器: MyNavController ,这个Controller是我的根控制器,并推送主UIViewControllerMyMainController ,当应用程序启动应用程序使用大约130 MB的内存,当我推一个新的控制器:内存上升到160然后我弹出这个控制器:内存仍然是160(确切地说是159)然后我推另一个视图控制器:内存高达190 MB ......内存永远不会下降。

  • 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 ? 我总是在我的属性(按钮,视图,customView,customObject ...)中使用strong,但是当我在第二级控制器中设置断点到dealloc方法时,我知道它被调用了,所以应该释放控制器吗?

  • 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 ! 我尝试了一些东西:我做了一个空的UIViewController并将我的xib中的视图设置为我的一个非释放控制器,因此它可能比清除更重,所以这个控制器没有代码行,只需一个.h和.m没有任何自定义代码,当我按下这个控制器时内存上升,当我弹出它时,内存不会下降! I really do not understand what i have to look for, do i have to llok for on my MainViewController ? 我真的不明白我需要寻找什么,我是否必须在我的MainViewController上进行llok? 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? 我总是在我的属性(按钮,视图,customView,customObject ...)中使用strong,但是当我在第二级控制器中设置断点到dealloc方法时,我知道它被调用了,所以应该释放控制器吗?

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. 如果调用该控制器的dealloc ,则会告诉您这不涉及任何保留周期,并且系统将恢复与控制器关联的内存。 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. IBOutlet ,通常你的IBOutlet引用(即由NIB /故事板创建的那些东西)在ARC项目中应该是weak的。


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. 通过静态分析器运行代码(Xcode“产品”菜单上的“分析”)并确保在那里没有警告。 If you have any, fix them first. 如果您有,请先修复它们。

  2. Run your code through the Leaks tool in Instruments and see if it reports anything. 通过Instruments中的Leaks工具运行代码,看看它是否报告任何内容。 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. 有关如何执行此操作的一些演示,请参阅WWDC 2012视频iOS应用程序性能:内存

Frankly, 130MB seems to be highly too much for a start in general. 坦率地说,130MB似乎对于一般的开始来说太过分了。

  • 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. 在ARC中也调用dealloc ,因此您可以在释放对象时注销。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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