- 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.