从代码中模拟内存警告,可能吗? [英] Simulate memory warnings from the code, possible?
问题描述
我知道我可以通过从iPhone Simulator的下拉菜单中选择"Simulate Memory Warning"来在模拟器上模拟内存警告.我什至可以为此设置一个热键.
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];
此方法将让App的UIApplication对象发布UIApplicationDidReceiveMemoryWarningNotification并调用App Delegate上的applicationDidReceiveMEmoryWarning:方法和所有UIViewController的
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屋!