简体   繁体   English

为什么保留人数如此之高? 内存管理

[英]Why is the retain count so high? Memory Management

I have been going back through my app trying to handle all the memory problems and reading up on memory management. 我一直在浏览我的应用程序,尝试处理所有内存问题并阅读内存管理。 I began using [object retainCount] to trace my memory allocation. 我开始使用[object keepCount]跟踪我的内存分配。 Is this to be trusted because I keep finding the counts really strange? 这是值得信赖的,因为我不断发现计数真的很奇怪吗?

Could someone explain the following: 有人可以解释以下内容:

Bear in mind this the app delegate and an empty mainViewController makes no difference. 请记住,此应用程序委托和一个空的mainViewController没有区别。 The initWithRootViewController is causing the count to go up, but I don't see another way of adding one.... initWithRootViewController导致计数增加,但是我看不到添加一个的另一种方法。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {      


/* Create the View Controllers */  
UIViewController *mainViewControl = [[[MainViewController alloc] init] autorelease];


/* Create the Navigation Controller */
UINavigationController *navigationController = [[[UINavigationController alloc] initWithRootViewController:mainViewControl] autorelease];


NSLog(@"retain count: %i",[mainViewControl retainCount]);

/* Set the toolbar to purple */
navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
navigationController.navigationBar.tintColor = [UIColor colorWithRed:.6 green:.1 blue:.4 alpha:0.4];
navigationController.navigationBar.translucent = YES;

NSLog(@"retain count: %i",[mainViewControl retainCount]);
navigationController.toolbar.barStyle = UIBarStyleBlackTranslucent;
navigationController.toolbar.tintColor = [UIColor colorWithRed:.6 green:.1 blue:.4 alpha:0.4];
navigationController.toolbar.translucent = YES;

[navigationController setNavigationBarHidden:YES animated:NO];
[navigationController setToolbarHidden:YES animated:NO];

NSLog(@"retain count: %i",[mainViewControl retainCount]);

[window addSubview:[navigationController view]];

NSLog(@"retain count: %i",[mainViewControl retainCount]);

And this is the log ~ 这是日志〜

2011-01-17 19:47:21.278 ANA[5653:207] 3
2011-01-17 19:47:21.282 ANA[5653:207] 4
2011-01-17 19:47:21.286 ANA[5653:207] 7
2011-01-17 19:47:21.287 ANA[5653:207] 12
2011-01-17 19:47:21.301 ANA[5653:207] Load View

I don't understand why changing those properties or referencing the navigationController is causing the retain count to shoot up. 我不明白为什么更改这些属性或引用navigationController导致保留计数激增。

I have done it without the autoreleases and manually released too but the result is the same. 我没有自动发布就完成了它,也手动发布了,但是结果是一样的。 Basically I don't get it, and wonder if the retainCount command is reliable, because if I can't understand this, I don't think I can debug any memory issues elsewhere... 基本上我不了解它,不知道keepCount命令是否可靠,因为如果我不理解这一点,我认为我无法调试其他地方的任何内存问题...

As stated in the official documentation for -retainCount , -retainCount官方文档-retainCount

Important: This method is typically of no value in debugging memory management issues. 重要:在调试内存管理问题时,此方法通常没有任何价值。 Because any number of framework objects may have retained an object in order to hold references to it, while at the same time autorelease pools may be holding any number of deferred releases on an object, it is very unlikely that you can get useful information from this method. 由于可能有许多框架对象保留了一个对象以保留对其的引用,而同时自动释放池可能会在一个对象上保留任何数量的延迟发布,因此从此获取有用信息的可能性很小方法。

Don't rely on -retainCount . 不要依赖-retainCount Other objects may be retaining your object without you knowing it, and autoreleased objects might give you a wrong impression of the actual retain count. 其他对象可能会在您不知道的情况下保留您的对象,而自动释放的对象可能会给您对实际保留计数的错误印象。

You can rely on Apple's framework objects to do the right thing and relinquish ownership of your object when appropriate, and you need to make sure you are doing that as well. 您可以依靠Apple的框架对象来做正确的事情,并在适当的时候放弃对对象的所有权,并且需要确保自己也在做。

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

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