iphone - 从代码中模拟内存警告,可能吗?

标签 iphone memory memory-management ios-simulator

我知道我可以通过从 iPhone 模拟器的下拉菜单中选择“模拟内存警告”来模拟模拟器上的内存警告。我什至可以为此制作一个热键。

但这不是我想要实现的。我想从代码中简单地做到这一点,假设每 5 秒做一次。这可能吗?

最佳答案

实际上这很简单,但是它依赖于一个未记录的 api 调用,所以不要随它一起发布你的应用程序(即使它位于无法访问的代码路径中)。您所要做的就是使用 [[UIApplication sharedApplication] _performMemoryWarning];

此方法将使应用的 UIApplication 对象发布 UIApplicationDidReceiveMemoryWarningNotification 并在 App Delegate 和所有 上调用 applicationDidReceiveMemoryWarning: 方法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
}

关于iphone - 从代码中模拟内存警告,可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2784892/

相关文章:

C++17 多态内存资源不工作

c++ - 堆栈内存是连续的吗?

objective-c - 何时以及为何释放对象

iphone - iOS 中的 System.IO.Path 等价物

iphone - [ios.cocos2d+box2d]如何关闭自动旋转?

java - 具有可点击区域的图像

PHP文件上传停止

c++ - 在堆对象的上下文中理解堆栈

php - 为现有网站开发移动应用程序(Iphone、Blackberry、Android)

iphone - '__strong' 仅适用于 objective-c 对象或 block 指针类型;这里输入的是 XXX”警告