Xcode分配工具 - 聆听内存警告

时间:2013-02-16 10:16:50

标签: iphone ios objective-c xcode memory-management

我正在使用分配工具来增强我的应用程序的性能。我想关注memoryWarnings,以确保我的应用程序不会消耗太多内存或崩溃。

我希望我的整个应用程序能够收听memoryWarings。我知道我可以用它来听某些警告,但下面的代码会听一切吗?另外,我需要在哪里实现它?我是否需要将其放在每个View Controller中,还是可以将其放入App Delegate?

- (id)init {
if ((self = [super init])) 
    {
    _cache = [NSMutableDictionary new];
    [[NSNotificationCenter defaultCenter] addObserver:self 
    selector:@selector(memoryWarning:) 
    name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
    }
return self;
}

我知道我需要实现侦听memoryWarnings的方法。这会听取所有记忆警告吗?另外,我需要将它放在每个viewController中吗?或者我可以在AppDelegate中以某种方式放置它吗?

- (void)memoryWarning:(NSNotification*)note {
    [_cache removeAllObjects];
}

任何指导都会很棒!谢谢!

1 个答案:

答案 0 :(得分:1)

您的视图控制器已有一种侦听内存警告的方法

 - (void)didReceiveMemoryWarning
 {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}
相关问题