25

I know i can simulate a memory warning on the simulator by selecting 'Simulate Memory Warning' from the drop down menu of the iPhone Simulator. I can even make a hot key for that.

But this is not what I'd like to achieve. I'd like to do that from the code by simply, lets say doing it every 5 seconds. Is that possible?

krasnyk
  • 3,458
  • 3
  • 24
  • 20

4 Answers4

59

It is pretty easy actually, however it relies on an undocumented api call, so dont ship your app with it (even if it is in a inaccessible code path). All you have to do is use [[UIApplication sharedApplication] _performMemoryWarning];.

This method will have the app's UIApplication object post the UIApplicationDidReceiveMemoryWarningNotification and call the applicationDidReceiveMemoryWarning: method on the App Delegate and all UIViewControllers.

-(IBAction) performFakeMemoryWarning {
  #ifdef DEBUG_BUILD
    SEL memoryWarningSel = @selector(_performMemoryWarning);
    if ([[UIApplication sharedApplication] respondsToSelector:memoryWarningSel]) {
      [[UIApplication sharedApplication] performSelector:memoryWarningSel];
    }else {
      NSLog(@"Whoops UIApplication no loger responds to -_performMemoryWarning");
    }
  #else
    NSLog(@"Warning: performFakeMemoryWarning called on a non debug build");
  #endif
}
Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
Brad The App Guy
  • 16,255
  • 2
  • 41
  • 60
  • Sadly this isn't working for me on 4.2, the respondsToSelector conditional resolves to true and the selector is performed but nothing happens. – Shizam Mar 07 '11 at 01:59
  • Still works for me on 4.3 (though I don't bother with the respondsToSelector). – smparkes Jun 28 '11 at 00:29
  • 4
    working for me on 5.0.1 `[[UIApplication sharedApplication] performSelector:@selector(_performMemoryWarning)];` – jasongregori Jan 27 '12 at 05:05
  • Make me nervous because it is undocumented. Therefore you can never be sure it will behave exactly like a real memory error... In any case this is a useful test even if you should not rely on it 100%. – Vic320 Mar 26 '12 at 16:23
  • You sir are a hero. Thank you. – Eugene Oct 24 '12 at 16:31
  • REMOVE this method call before submitting the app to Apple, otherwise it will be rejected. – Zubair May 06 '13 at 13:10
5

I wrote an apple script that will hammer the simulator with memory errors, it is a bit extreme but if your code survives, then you can be more confident...

on run
repeat 100 times
    tell application "System Events"
        tell process "iOS Simulator"
            tell menu bar 1
                tell menu bar item "Hardware"
                    tell menu "Hardware"
                        click menu item "Simulate Memory Warning"
                    end tell
                end tell
            end tell
        end tell
    end tell
    delay 0.5
end repeat
end run
John Riselvato
  • 12,854
  • 5
  • 62
  • 89
Vic320
  • 1,105
  • 2
  • 10
  • 22
4

Post a UIApplicationDidReceiveMemoryWarningNotification notification to the default notification center:

[[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationDidReceiveMemoryWarningNotification object:nil]
Bill
  • 44,502
  • 24
  • 122
  • 213
  • 1
    This will not work the same way as the memory warning triggered on a simulator with Hardware->Simulate Memory Warning. What is the difference? Your code will only post notification so that whenever you listen to this notification, you will of course get notified, but all those -didReceiveMemoryWarning methods of viewControllers and so on won't be called when your solution would be used. – krasnyk May 18 '11 at 09:22
  • 7
    wont work correctly. For correct work, use [[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationDidReceiveMemoryWarningNotification object: [UIApplication sharedApplication]]; – tt.Kilew Oct 05 '11 at 11:43
1

Just alloc-init big objects in a loop, and never release them. That should trigger a memory warning pretty quickly, I'd imagine.

Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345
  • 1
    Yeah that was another solution, but I'm looking to a more professional way of doing it. Ppl that decide to do it that way has also to keep in mind to allocate those objects in different thread, cause doing it in the main one would simply kill the application(cause it's not gonna come back to the main loop). – krasnyk May 06 '10 at 22:41
  • 3
    Just allocating memory doesn't do it, you actually have to write to the memory you allocated. I had written an app to try this and discovered that after allocating 300MB on a 3GS and it was still going. – progrmr May 07 '10 at 22:13
  • Did you `init` the `alloc`-ed object instance? This usually involves writing to memory, setting the default values of properties, etc. – Alex Reynolds Jan 11 '12 at 07:11
  • This might not be an elegant way of doing it but unlike some of the other methods, it is a very "real" case of low memory and all the funkiness that can cause. Low memory issues can be so tricky, that testing with all these methods would be the most air tight way of making sure you don't have any bugs. – Vic320 Mar 26 '12 at 16:28