xcode 如何调试iOS因内存压力而崩溃

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19253365/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:57:43  来源:igfitidea点击:

How to debug iOS crash due to memory pressure

xcodedebuggingcrashxcode-instruments

提问by andreapavan

I'm using ARC and the app crashes saying received memory warning. I'm testing the application directly on the device (iPhone 4 with iOS 7.0.2) and compiling with XCode 5 using iOS 6 SDK. I have used the apple instruments and I am having have around a 20MB LiveBytes allocated.

我正在使用 ARC 并且应用程序崩溃说收到内存警告。我直接在设备(带有 iOS 7.0.2 的 iPhone 4)上测试应用程序,并使用 iOS 6 SDK 使用 XCode 5 进行编译。我使用了苹果工具,并且分配了大约 20MB LiveBytes。

App at start

启动时的应用程序

After 4-5 min my app has 30mb of memory.

4-5 分钟后,我的应用程序有 30mb 的内存。

App status after 5 min

5 分钟后的应用状态

After compiling and testing the app on device I see that crash after a few minutes, just after the memory warning message. Why do not happen crash using instruments? However I am trying to clear this problem for a month and can't get thing rite, and I really need help. It looks like I do not have any leaks but I cannot find where is wrong. Thanks in advance for any advice.

在设备上编译和测试应用程序后,我看到几分钟后崩溃,就在内存警告消息之后。为什么使用仪器不会发生崩溃?但是,我试图用一个月的时间来解决这个问题,但无法解决问题,我真的需要帮助。看起来我没有任何泄漏,但我找不到哪里出了问题。提前感谢您的任何建议。

采纳答案by andreapavan

I solved the problem. In my case, the memory pressure, it was due to the constant memory usage by a run loop cycle. The loop is executed every second and works on data that must be analyzed and presented in the views. Another thing, the project initially was not using ARC. After a conversion project to ARC has occurred the problem.

我解决了这个问题。就我而言,内存压力是由于运行循环周期不断使用内存造成的。该循环每秒执行一次,处理必须在视图中分析和呈现的数据。另一件事,该项目最初没有使用 ARC。在将项目转换为 ARC 后,出现了问题。

Before the conversion of the project to ARC, at the end of the loop I had a direct call for the release of resources. With ARC of course this is done automatically, and the problem is just that. So for the class that runs the loop, I returned to non-ARC version and I used the tricks to make manually to release the resources that I used.

在项目转换为ARC之前,在循环结束时我直接调用了资源的释放。当然,对于 ARC,这是自动完成的,问题仅此而已。因此,对于运行循环的类,我返回到非 ARC 版本,并使用了手动 make 的技巧来释放我使用的资源。

Autorelease pool blocks provide a mechanism whereby you can relinquish ownership of an object, but avoid the possibility of it being deallocated immediately (such as when you return an object from a method). Typically, you don't need to create your own autorelease pool blocks, but there are some situations in which either you must or it is beneficial to do so.

自动释放池块提供了一种机制,您可以通过该机制放弃对象的所有权,但避免立即释放对象的可能性(例如当您从方法返回对象时)。通常,您不需要创建自己的自动释放池块,但在某些情况下,您必须这样做或这样做是有益的。

@autoreleasepool {
    // Code that creates autoreleased objects.
}

At the end of the autorelease pool block, objects that received an autorelease message within the block are sent a release message—an object receives a release message for each time it was sent an autorelease message within the block.

在自动释放池块的末尾,在块内收到自动释放消息的对象会被发送一个释放消息——一个对象在块内每次被发送自动释放消息时都会收到一个释放消息。

You can place an @autoreleasepool block around any section of code, however you really shouldn't do what I think you're doing.

您可以在代码的任何部分周围放置一个 @autoreleasepool 块,但是您真的不应该做我认为您正在做的事情。

Autorelease is much less efficient than allowing ARC to add in retain and release calls for you, and it's potentially unsafe. Autorelease puts all of your objects in a "pool" and then when you're out of scope and/or whenever it decides to dump the pool, it "drains" the pool and the objects' retain counts get decremented by one.

与允许 ARC 为您添加保留和释放调用相比,自动释放的效率要低得多,而且它可能不安全。Autorelease 将所有对象放入“池”中,然后当您超出范围和/或决定转储池时,它会“耗尽”池并且对象的保留计数减一。

The short answer: Leave out the @autorelease blocks completely unless Apple says otherwise in the documentation or the template (for example, main.m will have an @autoreleasepool in it).

简短的回答:除非 Apple 在文档或模板中另有说明(例如,main.m 中将包含一个 @autoreleasepool),否则完全省略 @autorelease 块。

This means that your objects could potentially get released before you really wanted them to. @autoreleasepool blocks are more useful for when you have a very tight loop of code that's going to instantiate and then discard a massive amount of objects. For example, a for loop that processes a huge database and allocates string objects and then uses those string objects to fill the properties of instances of a class you've created. In this case, ARC may not release those objects reliably while you're inside the for loop and you may need to create an autorelease pool.

这意味着您的对象可能会在您真正想要它们之前被释放。@autoreleasepool 块在您有一个非常紧凑的代码循环时更有用,这些代码将实例化然后丢弃大量对象。例如,for 循环处理一个巨大的数据库并分配字符串对象,然后使用这些字符串对象来填充您创建的类的实例的属性。在这种情况下,当您在 for 循环中时,ARC 可能无法可靠地释放这些对象,您可能需要创建一个自动释放池。

However, ARC not doing the right thing in a tight loop isn't very common. It's really more of a non-ARC concept, where you use an NSAutoreleasePool and you manually drain it.

但是,ARC 在紧密循环中没有做正确的事情并不常见。它实际上更像是一个非 ARC 概念,您使用 NSAutoreleasePool 并手动将其排空。

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAutoreleasePools.html#//apple_ref/doc/uid/20000047-CJBFBEDI

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAutoreleasePools.html#//apple_ref/doc/uid/20000047-CJBFBEDI

I hope I have helped others with the same problem.

我希望我能帮助其他有同样问题的人。

回答by Bhushan_pawar

 #pragma mark - Received Memory Warning

//memory pressure ios
- (void)didReceiveMemoryWarning
{
   [super didReceiveMemoryWarning];

    if ( [self isViewLoaded] && self.view.window == nil )
    {
        self.view = nil;
    }

    // Dispose of any resources that can be recreated.
}