从代码模拟内存警告,可能吗? [英] Simulate memory warnings from the code, possible?
问题描述
我知道我可以通过从 iPhone 模拟器的下拉菜单中选择模拟内存警告"来模拟模拟器上的内存警告.我什至可以为此制作一个热键.
I know i can simulate a memory warning on the simulator by selecting 'Simulate Memory Warning' from the drop down menu of the iPhone Simulator. I can even make a hot key for that.
但这不是我想要实现的.我想从代码中简单地做到这一点,假设每 5 秒执行一次.这可能吗?
But this is not what I'd like to achieve. I'd like to do that from the code by simply, lets say doing it every 5 seconds. Is that possible?
推荐答案
实际上很容易,但是它依赖于未记录的 api 调用,所以不要随它一起发布您的应用程序(即使它位于无法访问的代码路径中).你所要做的就是:[[UIApplication sharedApplication] _performMemoryWarning];
It is pretty easy actually, however it relies on an undocumented api call, so dont ship your app with it (even if it is in a inaccessible code path). All you have to do is: [[UIApplication sharedApplication] _performMemoryWarning];
此方法将使应用程序的 UIApplication 对象发布 UIApplicationDidReceiveMemoryWarningNotification 并调用应用程序委托和所有 UIViewController 上的 applicationDidReceiveMEmoryWarning: 方法
This method will have the App's UIApplication object post the UIApplicationDidReceiveMemoryWarningNotification and call the applicationDidReceiveMEmoryWarning: method on the App Delegate and all UIViewController's
-(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
}
这篇关于从代码模拟内存警告,可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!