我知道我可以通过从 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/