在测试我的iPhone应用程序时跟踪内存管理的最佳方法是什么?

时间:2021-12-08 20:16:25

While developing my app I have come to realize that the majority of my app crashes have arisen from poor memory management.

在开发我的应用程序时,我逐渐意识到我的大多数应用程序崩溃都源于糟糕的内存管理。

I understand I can print or log retain counts through NSLog (@"retain count is:%d",[myInstance retainCount]);

我知道我可以通过NSLog打印或记录保留计数(@“保留计数为:%d”,[myInstance retainCount]);

But isn't there a better, less manual method? Possibly a visual representation of your objects and instances?

但是,是不是有更好的,更少手动的方法?可能是对象和实例的直观表示?

answered. Cheers, Adam & Jason. :-)

回答。干杯,亚当和杰森。 :-)

4 个解决方案

#1


Use the Leaks and Object Allocation tools through XCode.

通过XCode使用Leaks和Object Allocation工具。

Run > Start with Performance Tool > ...

#2


In addition to the other answers, I would highly recommend using clang to do a static memory analysis of your code. It won't catch every memory-management error, but it does catch quite a lot. If your chief problem seems to come from memory management errors, clang will go a long way toward finding those errors. Clang is free, at http://clang.llvm.org/

除了其他答案,我强烈建议使用clang对代码进行静态内存分析。它不会捕获每个内存管理错误,但它确实捕获了很多。如果您的主要问题似乎来自内存管理错误,那么clang将在很大程度上找到这些错误。 Clang是免费的,网址是http://clang.llvm.org/

#3


As Adam suggests, Instruments is a very useful tool for these kinds of things. It's fairly easy to use, but can be a bit overwhelming at first. I suggest perusing the Instruments User Guide as you get started. It's pretty easy to follow and is helpful until you've used Instruments for a while. Even without reading the guide, however, Instruments is still far easier and more intuitive than littering your code with NSLog() calls and trying to parse the output yourself ;)

正如亚当所说,仪器对于这类事物来说是一个非常有用的工具。它相当容易使用,但起初可能有点压倒性。我建议您在开始时仔细阅读“仪器用户指南”。它很容易理解,并且在您使用仪器一段时间之前有所帮助。然而,即使没有阅读指南,仪器仍然比使用NSLog()调用乱丢代码并尝试自己解析输出更容易,更直观;)

#4


I also find the NSZombie trick useful for tracking down cases of over-release objects.

我还发现NSZombie技巧可用于跟踪过度释放对象的情况。

Basically the link describes a 'trick' so that release'd objects get replaced by NSZombie objects which if they get released again throw exceptions.

基本上,链接描述了一个“技巧”,以便释放的对象被NSZombie对象替换,如果它们再次被释放则抛出异常。

You can then use Instruments to track back to where the object was allocated.

然后,您可以使用Instruments跟踪回到对象的分配位置。

#1


Use the Leaks and Object Allocation tools through XCode.

通过XCode使用Leaks和Object Allocation工具。

Run > Start with Performance Tool > ...

#2


In addition to the other answers, I would highly recommend using clang to do a static memory analysis of your code. It won't catch every memory-management error, but it does catch quite a lot. If your chief problem seems to come from memory management errors, clang will go a long way toward finding those errors. Clang is free, at http://clang.llvm.org/

除了其他答案,我强烈建议使用clang对代码进行静态内存分析。它不会捕获每个内存管理错误,但它确实捕获了很多。如果您的主要问题似乎来自内存管理错误,那么clang将在很大程度上找到这些错误。 Clang是免费的,网址是http://clang.llvm.org/

#3


As Adam suggests, Instruments is a very useful tool for these kinds of things. It's fairly easy to use, but can be a bit overwhelming at first. I suggest perusing the Instruments User Guide as you get started. It's pretty easy to follow and is helpful until you've used Instruments for a while. Even without reading the guide, however, Instruments is still far easier and more intuitive than littering your code with NSLog() calls and trying to parse the output yourself ;)

正如亚当所说,仪器对于这类事物来说是一个非常有用的工具。它相当容易使用,但起初可能有点压倒性。我建议您在开始时仔细阅读“仪器用户指南”。它很容易理解,并且在您使用仪器一段时间之前有所帮助。然而,即使没有阅读指南,仪器仍然比使用NSLog()调用乱丢代码并尝试自己解析输出更容易,更直观;)

#4


I also find the NSZombie trick useful for tracking down cases of over-release objects.

我还发现NSZombie技巧可用于跟踪过度释放对象的情况。

Basically the link describes a 'trick' so that release'd objects get replaced by NSZombie objects which if they get released again throw exceptions.

基本上,链接描述了一个“技巧”,以便释放的对象被NSZombie对象替换,如果它们再次被释放则抛出异常。

You can then use Instruments to track back to where the object was allocated.

然后,您可以使用Instruments跟踪回到对象的分配位置。