ios iOS开发:我怎样才能引起对设备内存不足的警告?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4717138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS Development: How can I induce low memory warnings on device?
提问by BeachRunnerFred
I'd like to test my app functions well in low memory conditions, but it's difficult to test. How can I induce low memory warnings that trigger the didReceiveMemoryWarning method in my views when the app is running on the device, not the simulator? Or what are some ways I can test my app under these possible conditions?
我想在低内存条件下很好地测试我的应用程序功能,但很难测试。当应用程序在设备上而不是模拟器上运行时,如何在我的视图中引发触发 didReceiveMemoryWarning 方法的低内存警告?或者我可以通过哪些方式在这些可能的条件下测试我的应用程序?
The reason I can't use the simulator is my app uses Game Center and invites don't work on the simulator.
我不能使用模拟器的原因是我的应用程序使用了 Game Center 并且邀请在模拟器上不起作用。
采纳答案by BinaryStar
To test on a device, just add some code that periodically allocates large chunks of memory without freeing it (i.e. leak on purpose). You can do this in a separate thread, or in response to a timer, or using whatever mechanism that best allows you to test and observe the behavior of your application.
要在设备上进行测试,只需添加一些代码来定期分配大块内存而不释放它(即故意泄漏)。您可以在单独的线程中执行此操作,或者响应计时器,或者使用最适合您测试和观察应用程序行为的任何机制。
You might also choose to create a separate app that does something similar and is designed to run in the background, if you'd like to easily reuse this and/or test with multiple applications.
如果您想轻松地重用此应用程序和/或对多个应用程序进行测试,您也可以选择创建一个单独的应用程序,该应用程序执行类似的操作并设计为在后台运行。
回答by Enzo Tran
You can call the private method:
您可以调用私有方法:
[[UIApplication sharedApplication] performSelector:@selector(_performMemoryWarning)];
Just remember to use it on debug only, or else your app will get rejected.
请记住仅在调试时使用它,否则您的应用程序将被拒绝。
回答by Ole Begemann
The iOS Simulator's Simulate Memory Warningmenu item allows you to simulate a memory warning.
iOS Simulator 的Simulate Memory Warning菜单项允许您模拟内存警告。
回答by ThomasW
Using Instruments, use the menu item: Instrument -> Simulate Memory Warning.
使用 Instruments,使用菜单项:Instrument -> Simulate Memory Warning。
To use Instruments on your app from Xcode, use the Product -> Profile menu item.
要从 Xcode 在您的应用程序上使用 Instruments,请使用 Product -> Profile 菜单项。
回答by ChikabuZ
I've re-written Enzo Tran's answerin Swift:
我用Swift重写了Enzo Tran 的回答:
UIControl().sendAction(Selector(("_performMemoryWarning")), to: UIApplication.shared, for: nil)
回答by Daniel A. White
Theres a menu command that will invoke it.
有一个菜单命令可以调用它。
Hardware > Simulate Memory Warningfrom the simulator.
Hardware > Simulate Memory Warning从模拟器。
回答by Adobels
If someone, for whatever reason, tries to do this in Swift 4 - here is how to allocate 1.2 GB of ram.
如果有人出于任何原因尝试在 Swift 4 中执行此操作 - 以下是如何分配 1.2 GB 内存。
let d = Data.init(repeating: 100, count: 1200000000)
- This is helpful to trigger a warning alert in other apps
- 这有助于在其他应用程序中触发警告警报
回答by kindaian
Converted @ChikabuZ to swift 3:
将@ChikabuZ 转换为 swift 3:
UIControl().sendAction(Selector(("_performMemoryWarning")), to: UIApplication.shared, for: nil)
回答by ph1lb4
If someone, for whatever reason, tries to do this in Swift 3 - here is how to allocate 1.2 GB of ram.
如果有人出于任何原因尝试在 Swift 3 中执行此操作 - 这里是如何分配 1.2 GB 内存。
for i in 0...1200 {
var p: [UnsafeMutableRawPointer] = []
var allocatedMB = 0
p.append(malloc(1048576))
memset(p[allocatedMB], 0, 1048576);
allocatedMB += 1;
}
回答by Vishal Chaudhry
Swift 4:
斯威夫特 4:
UIApplication.shared.perform(Selector(("_performMemoryWarning")))
UIApplication.shared.perform(Selector(("_performMemoryWarning")))
Can execute the above in response to an event/notification.
可以响应事件/通知执行上述操作。

