简体   繁体   English

保留Count NSArray与NSMutableArray

[英]retain Count NSArray vs. NSMutableArray

Short Question with a code example: 使用代码示例的简短问题:

NSLog(@"%i", [[[NSArray alloc] init] retainCount]);
NSLog(@"%i", [[[NSMutableArray alloc] init] retainCount]);

Output: 输出:

2
1

Why is the retainCount from the NSArray and NSMutableArray different? 为什么NSArray和NSMutableArray中的retainCount不同?

Nobody outside of apple knows for sure (but I'm sure soon there will be somebody that claims he knows exactly why that happened). 苹果之外没有人确切知道(但我很快就会有人声称他确切知道为什么会发生这种情况)。
Maybe this happens because iOS is smart and it reuses empty NSArrays. 也许这是因为iOS很聪明并且它重用了空的NSArrays。 And obviously [[NSArray alloc] init] creates an empty array that is of no real use. 显然[[NSArray alloc] init]创建一个没有实际用途的空数组。 And since it`s not mutable (ie you can't add objects later, and it will be empty forever) all empty NSArrays pointers can reference the same object. 并且因为它不可变(即你以后不能添加对象,并且它将永远是空的)所有空的NSArrays指针都可以引用同一个对象。

The mutable one can't be reused because you can add objects to it. 可变的一个不能重用,因为你可以添加对象。


Do not use retainCount! 不要使用retainCount!

From the apple documentation : 苹果文档

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. 因为任何数量的框架对象可能保留了一个对象以保存对它的引用,而同时自动释放池可能在对象上保留任意数量的延迟版本,所以您不太可能从此获取有用信息方法。

To understand the fundamental rules of memory management that you must abide by, read “ Memory Management Rules ”. 要了解必须遵守的内存管理的基本规则,请阅读“ 内存管理规则 ”。 To diagnose memory management problems, use a suitable tool: 要诊断内存管理问题,请使用合适的工具:

  • The LLVM/Clang Static analyzer can typically find memory management problems even before you run your program. LLVM / Clang静态分析器通常可以在运行程序之前发现内存管理问题。
  • The Object Alloc instrument in the Instruments application (see Instruments User Guide ) can track object allocation and destruction. Instruments应用程序中的Object Alloc仪器(参见仪器用户指南 )可以跟踪对象分配和销毁。
  • Shark (see Shark User Guide ) also profiles memory allocations (amongst numerous other aspects of your program). Shark(参见Shark用户指南 )还描述了内存分配(在程序的许多其他方面)。

The reason is that [[NSArray alloc] init] returns the same object no matter how many times you call it. 原因是[[NSArray alloc] init]返回相同的对象,无论你调用它多少次。 Look at this code: 看看这段代码:

NSArray *array1 = [[NSArray alloc] init];
NSArray *array2 = [[NSArray alloc] init];
NSArray *array3 = [[NSArray alloc] init];
NSLog(@"\narray1: %p\narray2: %p\narray3: %p",
      array1, array2, array3);

The output is: 输出是:

array1: 0x10010cae0
array2: 0x10010cae0
array3: 0x10010cae0

This makes sense, because NSArray is immutable, and all empty arrays are identical. 这是有道理的,因为NSArray是不可变的,并且所有空数组都是相同的。 It looks like NSArray keeps an empty array handy for this purpose since the retain count for the array pointed to by array1, array2, and array3 is 4. 看起来NSArray为此目的保留了一个空数组,因为array1,array2和array3指向的数组的保留计数为4。

I don't disagree with @fluchtpunkt's answer, but I think it's safe to say that we know exactly why this happens. 我并不反对@ fluchtpunkt的答案,但我认为可以肯定地说我们确切知道为什么会这样。 I suppose you could say that nobody knows exactly why Apple chose to implement it this way, but it does seem like a good idea. 我想你可以说没有人知道为什么苹果选择以这种方式实现它,但它似乎是一个好主意。

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

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