18
14

More than 5 years have passed since last update.

iOSでMemory Warningのテスト

Last updated at Posted at 2015-02-05

iPhoneアプリでdidReceiveMemoryWarningとかをテストした時のメモ。

Memory Warningを発生させる

シミュレータ

iOS Simulatorのメニューにて

Hardware -> Simulate Memory Warning

で再現できます。
ショートカットは[command]+[shift]+M。

Screen_Shot_2015-02-05_at_20_31_11.png

実機

下記の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");
}

以上

18
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
14