iphone - Xcode Allocations Instrument - 监听内存警告

标签 iphone ios objective-c xcode memory-management

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

我希望我的整个应用程序能够监听 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/

相关文章:

iphone - 随机更改 UIButton 背景图片

ios - 如何使用 AVAudioRecorder 在 iPhone 上录制音频?

iphone - 音频本地化

java - 当我想要 NSNumber 的 intValue 时,如何防止自己意外使用它的指针?

ios - 此包无效 - Info.plist 中索引 327 处的无效 XML 字符 '0x10'

iphone - 应用程序内购买无法正常工作。它给出 "Can not connect to itunes"

ios - 自定义类的Objective-C类调用方法

objective-c - 在iOS7中,为什么选中的单元格的背景色总是灰色?

iphone - Flurry User ID 是邪恶的吗?

ios - 我什么时候应该在我的模型中使用可选字符串?