繁体   English   中英

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

[英]Xcode Allocations Instrument - Listening to Memory Warnings

我正在使用分配工具来增强应用程序的性能。 我要注意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];
}

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM