我正在使用分配工具来增强我的应用程序的性能。我想关注内存警告,以确保我的应用程序不会占用太多内存或崩溃。
我希望我的整个应用程序能够监听 memoryWarings。我知道我可以用它来监听某些警告,但是下面的代码会监听所有内容吗?另外,我需要在哪里实现它?我需要将其放在每个 View Controller 中还是可以将其放在应用程序委托(delegate)中?
- (id)init {
if ((self = [super init]))
{
_cache = [NSMutableDictionary new];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(memoryWarning:)
name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
}
return self;
}
我知道我需要实现监听内存警告的方法。这会听所有的内存警告吗?另外,我需要将其放置在每个 viewController 中吗?或者我可以以某种方式将其放置在 AppDelegate 中吗?
- (void)memoryWarning:(NSNotification*)note {
[_cache removeAllObjects];
}
任何指导都会很棒!谢谢!
最佳答案
你的 View Controller 已经有一个方法来监听内存警告
- (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.
}
关于iphone - Xcode Allocations Instrument - 监听内存警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14909180/