【发布时间】:2013-02-16 10:16:49
【问题描述】:
我正在使用分配工具来提高我的应用程序的性能。我想注意 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];
}
任何指导都会很棒!谢谢!
【问题讨论】:
-
我建议改变你的方法并为此使用NSCache。当应用程序内存不足时,它会自动删除不需要的缓存条目。
标签: iphone ios objective-c xcode memory-management