我的应用程序使用大量内存。通常它运行良好,但在一段时间内没有重新启动的已加载设备上,它将因臭名昭著的低内存错误而被抛弃。

我想回应 didReceiveMemoryWarning 并释放我的一些缓存。

但我有一个问题,我的应用程序基于 OpenGL ES 模板,并且没有视图控制器。它只有一个 App Delegate,它保存对 glView 的引用。

我能做什么来捕获 didReceiveMemoryWarning 留言以便我回复?

有帮助吗?

解决方案

这也可以在您的 应用程序代理.

-(void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
  NSLog(@"Received memory warning!");
}

其他提示

您还可以添加一个方法作为一个观察者,在你想要的任何类,对UIApplicationDidReceiveMemoryWarningNotification通知。的代码可能会喜欢这样的:

- (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;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top