美文网首页
iOS 常见内存泄露修复

iOS 常见内存泄露修复

作者: who_young | 来源:发表于2023-02-12 18:18 被阅读0次

    iOS 开发过程中内存泄露比较常见,如何发现这些内存泄露呢?我的建议是使用 AMLeaksFinder + Xcode instruments - leaks + Xcode Product - Analyze 去发现。这里不详述发现过程。
    常见的内存泄露和修复方法如下所述。

    block 内循环引用

    情形 1:明显的循环引用

    self.block = ^{
        self.str = @"leak";
    };
    

    修改方法:

    __weak typeof(self)weakSelf = self; 
    self.block = ^{
        weakSelf.str = @"leak";
    };
    

    情形 2:遗漏weakSelf的循环引用

    __weak typeof(self)weakSelf = self; 
    self.block = ^{
        weakSelf.str1 = @"1";
        self.str1 = @"3";
    };
    

    修改方法:

    __weak typeof(self)weakSelf = self; 
    self.block = ^{
        weakSelf.str1 = @"1";
        weakSelf.str1 = @"3";
    };
    

    情形 3:block内调用实例变量的循环引用

    __weak typeof(self)weakSelf = self; 
    self.block = ^{
        _str = @"1";
    };
    

    修改方法:

    __weak typeof(self)weakSelf = self; 
    self.block = ^{
        weakSelf->_str = @"1";
    };
    

    情形 4:block内嵌套block的循环引用

    __weak typeof(self)weakSelf = self;
    self.block = ^{
        __strong typeof(weakSelf)strongSelf = weakSelf;
        strongSelf.testObj = [TestObject new];
        strongSelf.testObj.completeBlock = ^{
            strongSelf.str = @"block";
        };
    };
    self.block();
    

    修改方法:

    __weak typeof(self)weakSelf = self;
    self.block = ^{
        __strong typeof(weakSelf)strongSelf = weakSelf;
        strongSelf.testObj = [TestObject new];
        strongSelf.testObj.completeBlock = ^{
            weakSelf.str = @"block";
        };
    };
    self.block();
    

    不用 weak 修饰 delegate 导致的内存泄露

    @property (nonatomic, strong) id<TestLeakDelegate> delegate;
    

    修改方法:

    @property (nonatomic, weak) id<TestLeakDelegate> delegate;
    

    使用 NSTimer 导致的内存泄露

    self.timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(triggerSelector) userInfo:nil repeats:YES];
    

    修改方法:使用 NSProxy 作为中间者,弱引用 self。参考 YYWeakProxy

    self.timer = [NSTimer timerWithTimeInterval:1.0 target:[YYWeakProxy proxyWithTarget:self] selector:@selector(triggerSelector) userInfo:nil repeats:YES];
    

    OC 运行时方法导致的内存泄露,要使用 free 释放内存

    Method *methods = class_copyMethodList([self class], &count);
    free(methods);
    
    unsigned int classCount = 0;
    Class *classes = objc_copyClassList(&classCount);
    free(classes);
    
    char *argumentTypeEncoding = method_copyArgumentType(method, argumentIndex);
    free(argumentTypeEncoding);
    
    Ivar *ivars = class_copyIvarList(tryClass, &ivarCount);
    free(ivars);
    
    Protocol *__unsafe_unretained *protocols = objc_copyProtocolList(&prcount);
    free(protocols);
    
    unsigned int pcount;
    objc_property_t *objcproperties = class_copyPropertyList(cls, &pcount);
    free(objcproperties);
    

    函数名包含create、alloc、copy字眼的以下类型都需要手动释放内存:

    • CG类型的对象,需要手动执行释放操作CGxxxRelease(name)。
      例如:
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray(); CGColorSpaceRelease(cs);
    
    CGImageRef image = [self.imageGenerator copyCGImageAtTime:time actualTime:NULL error:&error]; 
    CGImageRelease(image);
    
    CGImageRef imageRef = CGBitmapContextCreateImage(context);
    CGImageRelease(imageRef);
    
    CGContextRef ctx = CGBitmapContextCreate(data,width,height,8,rowBytes,colorSpace,kCGImageAlphaNoneSkipLast);
    CGContextRelease(ctx);
    
    
    • CF、CT类型对象,需要手动执行释放操作CFRelease(name)。例如:
    CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
    CFRelease(num);
    
    CTTypesetterRef typesetter = CTTypesetterCreateWithAttributedString((__bridge CFAttributedStringRef)_attributedText);
    CFRelease(typesetter);
    
    CTTextAlignment alignment = kCTLeftTextAlignment;
    CTParagraphStyleRef style = CTParagraphStyleCreate((CTParagraphStyleSetting[1]){
        {kCTParagraphStyleSpecifierAlignment, sizeof(alignment), &alignment}}
    },1);
    CFRelease(style);
    
    • C语言代码中的malloc/calloc,需要需要手动执行释放操作free(name)。例如:
    uint8_t* inputBuf = (uint8_t*)malloc(inputSize);
    free(inputBuf);
    
    void *buffer = calloc(bufferSize, 1);
    free(buffer);
    
    
    • CF、CT类型对象,需要手动执行释放操作CFRelease(name)。例如:
    CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
    CFRelease(num);
    
    CTTypesetterRef typesetter = CTTypesetterCreateWithAttributedString((__bridge CFAttributedStringRef)_attributedText);
    CFRelease(typesetter);
    
    CTTextAlignment alignment = kCTLeftTextAlignment;
    CTParagraphStyleRef style = CTParagraphStyleCreate((CTParagraphStyleSetting[1]){
        {kCTParagraphStyleSpecifierAlignment, sizeof(alignment), &alignment}}
    },1);
    CFRelease(style);
    
    • C语言代码中的malloc/calloc,需要需要手动执行释放操作free(name)。例如:
    uint8_t* inputBuf = (uint8_t*)malloc(inputSize);
    free(inputBuf);
    
    void *buffer = calloc(bufferSize, 1);
    free(buffer);
    

    子视图强引用父视图导致的内存泄露

    TestView *testV = [[TestView alloc] initWithFrame:CGRectMake(100, 200, 100, 100)];
    testV.block = ^{
        self;
    };
    [self.view addSubview:testV];
    

    修改方法:

    TestView *testV = [[TestView alloc] initWithFrame:CGRectMake(100, 200, 100, 100)];
    __weak typeof(self)weakSelf = self;
    testV.block = ^{
        weakSelf;
    };
    [self.view addSubview:testV];
    

    OC 对象不明显的持有的 block 强引用导致的内存泄露

    @interface TestObject : NSObject
    - (void)callTest:(NSString *)test block:(dispatch_block_t)block;
    @end
    
    @interface TestObject ()
    @property (nonatomic, copy) dispatch_block_t block;
    @end
    
    @implementation TestObject
    - (void)callTest:(NSString *)test block:(dispatch_block_t)block {
        self.block = block;
    }
    @end
    
    [self.testObj callTestTest:@"test" block:^{
        self;
    }];
    

    修改方法:

    __weak typeof(self)weakSelf = self;
    [self.testObj callTestTest:@"test" block:^{
        weakSelf;
    }];
    

    其他情形,如有补充,还请提出建议。

    相关文章

      网友评论

          本文标题:iOS 常见内存泄露修复

          本文链接:https://www.haomeiwen.com/subject/smvdkdtx.html