1. 程式人生 > >ios - 知識梳理(造成記憶體洩漏的原因)

ios - 知識梳理(造成記憶體洩漏的原因)

  • block的迴圈引用
[self.service requestData:^(id data)
{
    self.title = data[@"title"];
}];

這種情況就是典型的迴圈引用導致記憶體洩漏,self強引用service, service強引用了block,而block回撥時又呼叫了self,導致block強引用了self,造成迴圈,無法釋放。

解決方案

__weak typeof(self) weakSelf = self;
[self.service requestData:^(id data)
{
    __strong typeof(weakSelf) strongSelf = weakSelf;
    if (strongSelf)
        strongSelf.title = data[@"title"];
}];
  • delegate的迴圈引用
VC:
testView.delegate = self;
[self.view addSubview: testView];

如上若是testView的delegate屬性若為強引用例如(strong, retain)就會造成迴圈引用

解決方案使用 weak修飾。

  • NSTimer造成強引用
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(run) userInfo:nil repeats:YES];
    }
    return self;
}

- (void)run
{
    NSLog(@"time counter ++ ");
}

- (void)dealloc
{
    NSLog(@" --- dealloc ---- ");
}


- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self removeFromSuperview];
}

NSTimer的記憶體管理法則有點特別,它是基於runloop運作,runloop會對其進行強引用,timer在新增target的時候 又將self強引用,所以dealloc方法永遠不會呼叫。

  • 非物件的記憶體處理 

例如

但凡通過quarzt2d中帶有creat/copy/retain方法創建出來的值都必須手動的釋放

有兩種方法可以釋放前面建立的路徑:

(1)CGPathRelease(path);

(2)CFRelease(path);

  • 大範圍遍歷

大範圍遍歷的時候儘量建立自己的 autoReleasePool.