Avoid Using retainCount

objective-c: stack heap data(字符串池,整型,字符型等一些实例变量都存放在这里面)

java:stack heap data(只处理字符串池),integerCache(integer的缓冲区)

Avoid Using retainCount

A method defined by the NSObject protocol allows you to obtain the current retain count for an object(获得对象当前retain的个数):

- (NSUInteger)retainCount

However, ARC has deprecated this method. In fact, the compiler will throw an error if you try to call it when using ARC, just as it does with such methods as retain, release, and autorelease.ARC是不赞同使用这个方法,当你使用了ARC如果你再想调用这个方法那么编译时就会出错,只是因为ARC它有这样的方法是retain,release,autorelease。 Even though this method is officially deprecated, it is often misunderstood, and its use should be avoided. If you are not using ARC, which you really should be, it’s still possible to use this method without getting a compiler error. So it is still important to understand why this method is to be avoided.即使官方已经弃用了这个方法但它还是经常被误解,并且应该尽量避免使用。如果你不使用ARC那么你确实可以使用这个方法,但它仍然可能会导致编译错误,所以理解为什么尽量避免使用这个方法是很重要的。

The problem is, though, that the absolute retain count is often completely irrelevant and not what you need to know. Even if you use this method only for debug purposes, it’s usually not at all helpful.问题是这个绝对的retain count经常是不准确的的并且你也不需要知道它。即使你用这个方法是为了debug,但它通常也没有什么帮助。

example 1:

while ([object retainCount])
{
    [object release];
}

If the object was also in an autorelease pool at the time, when that pool is drained, the object is further released, and a crash will certainly occur.如果这个同时对象也在autorelease pool,当pool排出时,这个对象又被release一次,那么它肯定就会崩溃了。

example 2:

// Large "retain counts" of some objects (singletons)
NSString *string = @"Some string";
NSLog(@"string retainCount = %lu", [string retainCount]);

NSNumber *numberI = @1;
NSLog(@"numberI retainCount = %lu", [numberI retainCount]);

NSNumber *numberF = @3.141f;
NSLog(@"numberF retainCount = %lu", [numberF retainCount]);
string retainCount = 18446744073709551615
numberI retainCount = 9223372036854775807
numberF retainCount = 1
  1. The first number is 2^64 – 1 and the second number is 2^63 – 1. The retain counts of these objects are both very large because they are representing singleton objects. NSString is implemented as a singleton object, if possible. It is possible if the string is a compile- time constant, as in the example. In this case, the compiler makes a special object, placing the data for the NSString object within the application binary, and uses that instead of creating an NSString object at runtime. NSNumber does a similar thing, using a concept known as tagged pointers for certain types of values.第一个数是2的64次方减1,第二个数是2的63次方减1.这两个对象的retain counts都是非常大的,因为它们代表一个单独的对象。在上面的例子里,编译的是一个特殊的对象(这个字符串存放在data里面)

  2. This optimization is done only for certain cases, though, which is why the floating-point number in the example has a retain count of 1, as it does not use this optimization.optimization仅仅只在一些特定的例子里面,这是为什么floating-point 的个数是1,只是因为它没有使用optimization。

  3. In addition, the retain counts of singletons such as these never change. Retains and releases are no-ops.

example 3:

// Showing retain count might not necessarily be what you expect
id object = [self createObject];
[opaqueObject doSomethingWithObject:object];
NSLog(@"retainCount = %lu", [object retainCount]);

What is the retain count? It could be anything. The call to doSomethingWithObject: may have added the object to multiple collections, retaining it in the process. Or the call may have retained the object multiple times and autoreleased it multiple times, some of which are still pending an autorelease pool drain. The retain count is therefore unlikely to be useful to you.在上面的代码里,doSomethingWithObject方法接收了这个对象你不能确定在这个方法里面object是否也调用了retain,所以你调用retainCount的结果可能是不准确的。