iOS之内存管理

2018-03-04  本文已影响45人  RasonWu
  1. OC知识--彻底理解内存管理(MRC、ARC)
    有时间的话,看完上面OC部分就差不多了。当引用计数为0时,对象被释放。堆和栈的概念也是要有大概的认识,可以这么认为堆是程序员需要管理的内存,栈是系统管理的内存。如果申请了内存,没有释放,就会造成浪费,造成内存不足,由于内存有限,所以需要管理
    for (int i=0; i<100000; i++) {
        @autoreleasepool{\\如果obj在后面没有再使用,要及时添加autoreleasepool可以及时减少内存的占用
            NSMutableString *obj = [NSMutableString stringWithFormat:@"sdfkjlas;dfj%i",i];
        }
    }
- 
CFGetRetainCount((__bridge CFTypeRef)(obj))
- (void)loopThroughDictionary:(NSDictionary *)dict error:(NSError **)error
{
    [dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop){

          @autoreleasepool  // 被隐式创建
      {
              if (there is some error && error != nil)
              {
                    *error = [NSError errorWithDomain:@"MyError" code:1 userInfo:nil];
              }
          }
    }];

    // *error 在这里已经被dict的做枚举遍历时创建的autorelease pool释放掉了 :(  
}  
    __weak NSString *str1 = @"my string33333sdfssdstring22222f";
    __weak NSString *str2 = [NSString stringWithString:@"my string22222ststring22222ring22222"];
    __weak NSString *str3 = [[NSString alloc] initWithString:@"my strinstring22222string22222g22222"];
    NSString *str4 = [[NSString alloc]initWithFormat:@"my stri"];
    NSMutableString *str5 = [NSMutableString stringWithString:@"my string"];
    NSMutableString *str6 = [[NSMutableString alloc] initWithString:@"my string"];
    NSMutableString *str7 = [[NSMutableString alloc]initWithFormat:@"my string"];
    NSLog(@"str1:%p----%ld--@\"my string\"",str1,[self getRetainCount:str1]);
    NSLog(@"str2:%p----%ld--stringWithString",str2,[self getRetainCount:str2]);
    NSLog(@"str3:%p----%ld--initWithString",str3,[self getRetainCount:str3]);
    NSLog(@"str4:%p----%ld--initWithFormat",str4,[self getRetainCount:str4]);
    NSLog(@"str5:%p----%ld--stringWithString",str5,[self getRetainCount:str5]);
    NSLog(@"str6:%p----%ld--initWithString",str6,[self getRetainCount:str6]);
    NSLog(@"str7:%p----%ld--initWithFormat",str7,[self getRetainCount:str7]);
    //跟str4做对照,NSString的initWithFormat会根据字符串长短,来判断是否配置无限大的引用计数
    NSString *str8 = [[NSString alloc]initWithFormat:@"my stri234sdfafasdfasadfjklj;lkjsdf"];
    NSLog(@"str8:%p----%ld--initWithFormat",str8,[self getRetainCount:str8]);
    //马上被释放,会崩溃
    __weak NSString *str9 = [[NSString alloc]initWithFormat:@"my stri2ssssssssssssss"];
    NSLog(@"str9:%p----%ld--initWithFormat",str9,[self getRetainCount:str9]);
    
}

下面是结果,引用计数居然出现1152921504606846975其实就是LONG_MAX的,但是就是这样的。常量不符合ARC的一般规则,是无限大,并且引用计数不受retain和release的影响。

2018-03-03 18:22:10.290594+0800 RSRuntimeMethodDemo[4021:479475] str1:0x1026bb1d8----1152921504606846975--@"my string"
2018-03-03 18:22:10.290754+0800 RSRuntimeMethodDemo[4021:479475] str2:0x1026bb1f8----1152921504606846975--stringWithString
2018-03-03 18:22:10.290892+0800 RSRuntimeMethodDemo[4021:479475] str3:0x1026bb218----1152921504606846975--initWithString
2018-03-03 18:22:10.290991+0800 RSRuntimeMethodDemo[4021:479475] str4:0xa6972747320796d7----9223372036854775807--initWithFormat
2018-03-03 18:22:10.291123+0800 RSRuntimeMethodDemo[4021:479475] str5:0x604000447ef0----2--stringWithString
2018-03-03 18:22:10.291243+0800 RSRuntimeMethodDemo[4021:479475] str6:0x604000447f80----1--initWithString
2018-03-03 18:22:10.291361+0800 RSRuntimeMethodDemo[4021:479475] str7:0x604000447f50----1--initWithFormat
2018-03-03 18:22:10.291503+0800 RSRuntimeMethodDemo[4021:479475] str8:0x608000074740----1--initWithFormat
  1. 内存管理当然少不了c语言的部分,malloc()和free()的原理
上一篇下一篇

猜你喜欢

热点阅读