6

My app uses a lot of memory. Normally it runs fine, but on a loaded device that hasn't been restarted in awhile, it will be jettisoned with the infamous Low Memory error.

I would like to respond to didReceiveMemoryWarning and free up some of my caches.

But I have the problem that my app is based off of the OpenGL ES template and doesn't have a view controller. It just has the App Delegate which holds a reference to the glView.

What can I do to trap the didReceiveMemoryWarning message so that I can respond?

Frank Krueger
  • 69,552
  • 46
  • 163
  • 208

2 Answers2

10

You can also add a method as an observer, in any class you want, to the UIApplicationDidReceiveMemoryWarningNotification notification. The code might like like this:

- (void) cleanMemory: (NSNotification*) notification {
  // Save memory!
}

- (id) init {  // Or any other function called early on.
  // other init code
  [[NSNotificationCenter defaultCenter]
   addObserver:self selector:@selector(cleanMemory:)
          name:UIApplicationDidReceiveMemoryWarningNotification
        object:nil];
  return self;
}
Abdul Jamil
  • 363
  • 3
  • 13
Tyler
  • 28,498
  • 11
  • 90
  • 106
9

This is also available within your Application Delegate.

-(void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
  NSLog(@"Received memory warning!");
}
NWCoder
  • 5,296
  • 1
  • 26
  • 23