简体   繁体   English

在Objective-C(iOS)中测试内存管理

[英]Testing memory management in Objective-C (iOS)

I'm somewhat new to Objective-C and while I think I've got a decent grasp of memory management, I'd like to become familiar with the best ways to test this (if possible). 我对Objective-C有些新意,虽然我认为我对内存管理有很好的把握,但我想熟悉一下测试它的最佳方法(如果可能的话)。

I know of two options: 1) Build and Analyze (from within Xcode) and 2) Instruments. 我知道两个选项:1)构建和分析(从Xcode内部)和2)仪器。

Are these the best tools for the job? 这些是这项工作的最佳工具吗? How accurate is the Build and Analyze feature? 构建和分析功能的准确性如何? I've not used Instruments yet, but it looked somewhat complex when I tried to get started. 我还没有使用乐器,但是当我试图开始时它看起来有些复杂。

I realize the best approach might be to study the Objective-C language itself, but I'm looking for supplemental ways to test this. 我意识到最好的方法可能是研究Objective-C语言本身,但我正在寻找补充方法来测试它。

Thanks for any insights. 感谢您的任何见解。

I see a lot of people stating that "Instruments can track down leaks [in context of memory management]" and leaving it at that. 我看到很多人都说“仪器可以追踪泄漏[在内存管理的背景下]”并将其留在那里。

So far from the truth; 远离真相; the Leaks analysis is useful, but barely scratches the surface of the awesome optimization opportunities it can reveal. 泄漏分析很有用,但几乎没有表现出它可以揭示的令人敬畏的优化机会。

  • Can be used for Zombie detection; 可用于僵尸检测; ie to track down the over-release of an object that causes a crash 即追踪导致崩溃的物体的过度释放

  • Can monitor the allocation bandwidth; 可以监控分配带宽; the # of objects your app creates/deallocates over time. 您的应用随着时间的推移创建/解除分配的对象数量。 Reducing that is a very effective means of optimizing an app. 减少这是优化应用程序的一种非常有效的方法。

  • Can inspect the exact set of objects live and in memory at any one time; 可以在任何时间检查实时和内存中的确切对象集; the working set. 工作集。

  • Using Heapshot analysis , can show you exactly how your app permanently grows over time. 使用快照分析 ,可以准确显示您的应用随着时间的推移永久增长。

  • Can tell you exactly where any given object is retained and/or released, including backtraces. 可以准确地告诉您保留和/或释放任何给定对象的位置,包括回溯。

The static analyser will attempt to find errors in your code by inspecting it without running it. 静态分析器将通过检查代码而不运行代码来尝试查找代码中的错误。 It's really good at spotting times when you accidentally make an allocation error. 当您意外发生分配错误时,它非常擅长发现时间。 It'll tell you about things that are unarguably going to cause a problem and occasions when you've stepped outside of normal Objective-C conventions. 它会告诉你无可争议的事情会导致问题,以及当你超出正常的Objective-C约定时的情况。 So don't necessarily be confused if it highlights something you think won't lead to a problem — Objective-C is a combination of rules and conventions and the analyser looks for breaches of both. 因此,如果它突出显示您认为不会导致问题的内容,则不一定会感到困惑 - Objective-C是规则和约定的组合,而分析器会查找两者的违规行为。

Instruments will look for leaked objects at runtime. 仪器将在运行时查找泄漏的对象。 So it can find only problems that actually occur. 所以它只能找到实际发生的问题。 So in principle you'd need to go through every single potential usage path in your program to catch everything. 因此,原则上您需要遍历程序中的每个潜在使用路径以捕获所有内容。 It tends to be really useful in practise though. 但它在实践中往往非常有用。 And it's not that complicated to use. 而且使用起来并不复杂。 Just launch with the leaks instrument, run for a bit, you'll get a graph of when memory is leaked and you can find out what sort of objects are being leaked. 只需使用泄漏仪器启动,运行一段时间,您将获得内存泄漏的图表,您可以找到泄漏的物体类型。 You can then ask to be shown where in the source the object was created. 然后,您可以要求显示在源中创建对象的位置。 Keep in mind that you'll get some secondary leaks (in that, if one object leaks then all the objects it was retaining will also leak). 请记住,你会得到一些二次泄漏(其中,如果一个物体泄漏,那么它所保留的所有物体也会泄漏)。

On the desktop Guard Malloc is also available, though I think it isn't yet on iOS. 在桌面上也可以使用Guard Malloc,但我认为它尚未在iOS上使用。 That causes your program to run a lot more slowly but will cause it to crash immediately upon any memory access error. 这会导致程序运行速度慢得多,但会在任何内存访问错误时立即崩溃。 So that's really helpful because normally things like writing past the end of C arrays or accessing deallocated objects may cause an error but won't necessarily do so, sometimes causing bugs in some part of the code to corrupt structures used by other parts of the code and cause a crash in code that's completely valid. 所以这真的很有帮助,因为通常写过C数组的末尾或访问解除分配的对象之类的东西可能会导致错误,但不一定会这样做,有时会导致代码的某些部分出现错误,从而破坏代码其他部分使用的结构并导致完全有效的代码崩溃。

Yeah the best thing you can do is to learn more about memory management http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/MemoryMgmt/MemoryMgmt.html 是的,你能做的最好的事情是了解更多关于内存管理的信息http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/MemoryMgmt/MemoryMgmt.html

Build and Analyze works pretty well for determining some things like if something is never released. 构建和分析非常适用于确定某些事情,例如从未发布某些内容。 It will help you track down alot of problems, but it is by no means perfect. 它将帮助您追踪很多问题,但它绝不是完美的。

Instruments will help you track down leaks but it is a little harder to use and track down exactly where things go wrong. 仪器将帮助您追踪泄漏,但它有点难以使用,并准确追踪出错的地方。

Another helpful tool you can use to track down crashes from memory is to click Run->Stop On Objective C Exceptions. 您可以用来跟踪内存崩溃的另一个有用工具是单击Run-> Stop On Objective C Exceptions。 Which will let you go through stack traces of where your application crashes to find out where it goes wrong. 这将让您浏览应用程序崩溃的堆栈跟踪,以找出出错的位置。

I'm fairly new to Objective-C (come from a Java background) but I was able to get by with the two features you mentioned (Build/Analyze) and Instruments. 我是Objective-C的新手(来自Java背景),但我能够使用你提到的两个功能(Build / Analyze)和Instruments。 I think those worked because I got my app approved by Apple and haven't had any customer complaints so far, so at least that's something. 我认为这些是有效的,因为我得到了苹果批准的应用程序,到目前为止还没有任何客户投诉,所以至少这是件事。

Anyway I found Analyze to be far less helpful than I would have thought. 无论如何,我发现Analyze远没有我想象的那么有用。 Mostly I found problems by using Allocations and Leaks Instruments. 大多数情况下,我通过使用Allocations和Leaks Instruments发现了问题。

Analyze didn't find things that I think they could probably check for. 分析没有找到我认为他们可以检查的东西。 An example would be not releasing (or autoreleasing) an object that you alloc/init and then assign to a retained property. 一个示例是不释放(或自动释放)您分配/初始化的对象,然后分配给保留属性。 You can even search your source for every time you use alloc and then just do a double check to make sure you're doing correct memory management for each case. 您甚至可以在每次使用alloc时搜索源代码,然后只需进行一次双重检查以确保为每种情况进行正确的内存管理。

Leaks instrument was very helpful, but make sure you put your app through the ringer while its running. 泄漏仪器非常有用,但请确保您的应用程序在运行时通过振铃器。 Just click on all sorts of stuff, go in and out of screen lots of times, click on things people would never do, etc. You will find the leaks. 只需点击各种各样的东西,多次进出屏幕,点击人们永远不会做的事情等等。你会发现泄漏。 Then crush them. 然后粉碎他们。 Very satisfying. 非常满意。

Clang with xcode is another great option. 与xcode一起使用是另一个很好的选择。 http://clang-analyzer.llvm.org/xcode.html http://clang-analyzer.llvm.org/xcode.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM