How to trigger a memory alert

What's the easiest way to trigger a memory alert?


source to share

4 answers

[[NSNotificationCenter defaultCenter] postNotificationName:                     
    @"UIApplicationMemoryWarningNotification" object:[UIApplication sharedApplication]];




In the simulator, you can go to the Hardware section and select Simulate Memory Alert.

enter image description here

If you're trying to do this on a real iOS device, this blog post explains how to send a memory alert to your code.



I like to put something hidden in my application in debug mode, such as triple clicking on a specific area of ​​my interface, which calls this:

- (void) simulateMemoryWarning:(UITapGestureRecognizer *)gesture {
[[NSNotificationCenter defaultCenter] postNotificationName:TriggerManualMemoryWarningNotification object:nil];



Then in my app div:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveManualMemoryWarning:) name:TriggerManualMemoryWarningNotification object:nil];



- (void) didReceiveManualMemoryWarning:(NSNotification *)notification {
 #ifdef DEBUG
    SEL memoryWarningSel = @selector(_performMemoryWarning);
    if ([[UIApplication sharedApplication] respondsToSelector:memoryWarningSel]) {
        [[UIApplication sharedApplication] performSelector:memoryWarningSel];
    }else {
        NSLog(@"%@",@"Whoops UIApplication no loger responds to -_performMemoryWarning");
    NSLog(@"%@",@"Warning: performFakeMemoryWarning called on a non debug build");




In the simulator, you can simulate one ...

From the device, you can allocate huge amounts of memory (eg through malloc

). You will need to do this step by step, otherwise the application might just crash without a memory alert.



All Articles