xcode IOS 应用程序因内存不足而被杀死,但未收到内存警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8445187/
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 app killed for Low Memory but no Memory Warning received
提问by user1089658
I'm stuck with a problem for some time now and I would like to know if anyone can help. I'm developing an IOS app (iPad) using a main navigation controller and a lot of UIImage. After using the application for a while, the app get killed for Low Memory (not in a particular View) but by checking the iPad log, I don't always see a Low Memory Warning (sometimes I do, sometimes I do not). Even when I receive one, it's just "Low Memory Warning received" but I never get "Level 1" or "Level 2".
我被一个问题困扰了一段时间,我想知道是否有人可以提供帮助。我正在使用主导航控制器和大量 UIImage 开发 IOS 应用程序(iPad)。使用该应用程序一段时间后,该应用程序因内存不足(不在特定视图中)而被杀死,但通过检查 iPad 日志,我并不总是看到内存不足警告(有时会,有时不会)。即使我收到一个,也只是“收到低内存警告”,但我从来没有得到“ 1 级”或“ 2 级”。
By using the Leak instrument from xCode, I couldn't find any leaks.
通过使用 xCode 的 Leak 工具,我找不到任何泄漏。
Can anyone help ?
任何人都可以帮忙吗?
回答by Tommy
A memory warning is sent as a notification, so it'll be queued up on the runloop for dispatch as soon as an opportunity arises. If you were to write a (deliberately broken) loop like:
内存警告作为通知发送,因此一旦机会出现,它将在运行循环中排队等待调度。如果您要编写一个(故意损坏的)循环,例如:
while(1)
{
NSString *newString = [NSString string];
}
Then eventually your app would be killed due to low memory but at no opportunity would it be in a position to receive a low memory warning.
然后最终您的应用程序将因内存不足而被终止,但它绝不会收到内存不足警告。
If you're being killed due to low memory without receiving a warning then you've probably creating a memory bottleneck for yourself somewhere, likely you have some sort of loop that leaves a lot of things in the autorelease pool — so, if you get all the way through the loop then the temporary objects vanish and hence have no long-term footprint, but they're accumulating all the time you remain in the loop.
如果您因内存不足而没有收到警告而被杀死,那么您可能在某处为自己创建了内存瓶颈,可能您有某种循环在自动释放池中留下了很多东西 - 所以,如果你得到一直通过循环,然后临时对象消失,因此没有长期足迹,但是它们一直在您留在循环中的时候积累。
To avoid that sort of situation you want to look at nesting inner parts of the loop in their own NSAutoreleasePool
s. For example, this loop:
为了避免这种情况,您需要查看将循环的内部部分嵌套在它们自己的NSAutoreleasePool
s 中。例如,这个循环:
while(1)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *newString = [NSString string];
[pool drain]; // stylistically preferred to release, but equivalent
// in reference counted environments
}
Will continue forever but won't ever trigger a low memory condition.
将永远继续,但永远不会触发低内存条件。
回答by sKhan
You can also try for memory leak, Apple's Xcode development environment provide a tools for memory leak detection, the easiest way to run it is straight from
也可以试试内存泄漏,苹果的Xcode开发环境提供了内存泄漏检测工具,最简单的运行方式就是直接从
Xcode: 1.Product -> 2.Start with Performance Tool(Profiler) -> 3.From instrument select Leaks.
Xcode: 1.Product -> 2.Start with Performance Tool(Profiler) -> 3.From instrument 选择 Leaks。
It seems very good at detecting memory leaks, and easy to figure out.
它似乎非常擅长检测内存泄漏,并且很容易弄清楚。