views:

295

answers:

2

I've seen some apps that generate a warning when low memory is detected. I tried to do this in my app but ran into a problem. Using the simulator to simulate a memory warning, the alert generated pops up twice before I can hit "ok" and pops up 9 more times after that times before it finally goes away.

Is it a bad idea to generate an alert when didReceiveMemoryWarning is called?

If not, is there a better way to do this than what I have below?

- (void)didReceiveMemoryWarning {

     [super didReceiveMemoryWarning];

     // Release any cached data, images, etc that aren't in use.
     ...

     UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"Warning" 
                          message:@"Your device is low on memory..." 
                          delegate:nil 
                          cancelButtonTitle:@"OK" 
                          otherButtonTitles:nil];
    [alert show];
    [alert release];    
}

Thanks,

phil

+4  A: 

Generally speaking, you shouldn't notify a user about low memory. After all, what can they do? Your app is the foreground app, which (aside from the Apple apps) is consuming most of the device's memory. What is the user going to do when they see the memory message?

When you get a low memory notification, you should solely focus on freeing memory, without user interaction.

Mike
A: 

As I understand it, you'll likely get this just before your app crashes (or is killed). If you manage to free up memory, the app may survive (but isn't guaranteed to).

There might be a few legitimate use cases. Since it's usually a prelude to a crash, you might want to warn the user of this. This could take the form of a message like: "Your device is low on memory. As a result, this application may unexpectedly exit. If the problem persists, try powering the phone off and on again". Assuming all is well, this shouldn't appear - so you may want some warning if it does.

This might be more common on jailbroken phones, where the popular 'backgrounder' app allows applications to be run in the background (often until this very condition occurs - at which point they're forcibly exited - see the comment here for example).

To work around the problem of the event being triggered 11 times consecutively.. You could rate limit the popup. For example: when you display, store the seconds since the epoch. Then - check that a certain amount of time has passed before displaying it again.

The use case you describe is what I'm trying to accomplish. I have had some users who have jailbroken phones write about my app crashing. If available memory is the issue, I'd like to let them know.I followed your suggestion to rate limit the alerts by moving them from the didReceiveMemoryWarning in each view controller to a common method in the app delegate where the rate limiting takes place. (It didn't dawn on me that the reason I was getting the repeated alerts was due to the didReceiveMemoryWarning firing in all view controllers at once until I made the change.) Thanks!
pirey4