Chinaunix首页 | 论坛 | 博客
  • 博客访问: 534068
  • 博文数量: 78
  • 博客积分: 1913
  • 博客等级: 上尉
  • 技术积分: 829
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-14 21:29
文章分类

全部博文(78)

文章存档

2011年(27)

2010年(26)

2009年(20)

2008年(5)

我的朋友

分类: C/C++

2009-11-11 18:17:26

In Xcode, create a command line project with a name NSStringRetainCountTest. And the implementation file is as follows:




#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
     /* Test Sample 1 */   

    NSString *a = [[NSString alloc]initWithString:@"Hello, string A."];

    NSLog(@"Retain count of string a is:%d", [a retainCount]);

    [pool drain];
    return 0;
}


And the OUTPUT result is:


2009-11-11 16:44:34.890 NSStringRetainCountTest[945:10b] Retain count of string a is:2147483647


    In Cocoa, before MAC 10.5, we cannot use GC(Garbage Collector), so retainCount(Reference Count Mechanism) is a useful memory manager mechanism. Also, even the GC is supported for mac os x, use the retainCount is a good choice to improve ur applications' performance.

    Maybe u will think the test is strange, when we use alloc/init method to create a object for a class, the retaincount must be 1, but in NSString, we got a very large number: 2147483647, Why?
    The reason is that the @"Hello, string A." a Constant String, the pointer 'a' points to this string, so, obj-c think:"Aha, this pointer cannot be release, so give him the retaincount value INT_MAX, but why use INT_MAX? I think this is a suggested value given by the Cocoa developers". SO, when our code is:

    NSString *cc = @"what what what";
    NSString *dd = [NSString stringWithString:@"anything"];
    ...


the result is the same!
    
   But which can release? Let's see the follows' code(we add test sample 2):

    /* Test Sample 2 */
    NSString *b = [[NSString alloc]initWithFormat:@"%s", @"Hello, string B."];
    NSLog(@"Retain count of string b is:%d", [b retainCount]);


   And this sample's OUTPUT result is:

2009-11-11 17:10:56.773 NSStringRetainCountTest[1088:10b] Retain count of string b is:1

  
   The method "initWithFormat:" is a mutable operation, it will copy the strings to a allocated memory. So, the retainCount is 1; It's a little strange, yeah? If we change the code like this:


    /* Test Sample 2 */
    NSString *= [[NSString alloc]initWithFormat:@"Aha,%s", @"Hello, string B."];
    NSLog(@"Retain count of string b is:%d", [b retainCount]);



    And now, we can see, @"Hello, string B." is a constant string, that's right, but it appended to @"Aha,", a new string @"Aha, Hello, string B." is created, that's to say, a copy of @"Hello, string B."is created, and this stored into the allocated memory, so the retaincount is 1.


    BUT the Most Strange Thing is this CODE:



    char *cchar = "A1";
    NSString *strss=[[NSString alloc]initWithUTF8String:cchar];
    NSLog(@"rc of strss is: %d", [strss retainCount]);
    
    char *cchar2 = "A";// or = "";

    NSString *strss2=[[NSString alloc]initWithUTF8String:cchar2];
    NSLog(@"rc of strss is: %d", [strss2 retainCount]);


    The OUTPUT Result is:

2009-11-11 18:16:32.231 NSStringRetainCountTest[2045:10b] rc of strss is: 1
2009-11-11 18:16:32.232 NSStringRetainCountTest[2045:10b] rc of strss is: 2147483647


   Who can help me?

阅读(1609) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~