iPhoneアプリでdidReceiveMemoryWarning
とかをテストした時のメモ。
Memory Warningを発生させる
シミュレータ
iOS Simulatorのメニューにて
Hardware -> Simulate Memory Warning
で再現できます。
ショートカットは[command]+[shift]+M。
実機
下記のprivate APIが使えます。
someFile.m
[[UIApplication sharedApplication] performSelector:@selector(_performMemoryWarning)];
Memory Warningをキャッチ
Memory Warningはそれぞれ
- AppDelegate
- ViewController
- UIApplicationDidReceiveMemoryWarningNotification
でキャッチできます。
AppDelegate.m
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
NSLog(@"applicationDidReceiveMemoryWarning");
}
ViewController.m
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
NSLog(@"didReceiveMemoryWarning");
}
someFile.m
- (void)someFunction
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleMemoryWarning:) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
}
- (void) handleMemoryWarning:(NSNotification *)notification
{
NSLog(@"handleMemoryWarning");
}
以上