xcode 帮助查找内存泄漏(一般提示)

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

Help finding memory leaks (general tips)

xcodeiosmemory-leaksinstruments

提问by Morrowless

This is on iOS, device is iPad.

这是在 iOS 上,设备是 iPad。

Here's what happens:

这是发生的事情:

  1. I run app on device or debug and run using Xcode.
  2. After 5 minutes I receive a memory warning of level 1.
  3. A minute later I receive a memory warning of level 2.
  4. Another minute later, Program received signal: “0”.
  1. 我在设备上运行应用程序或使用 Xcode 调试和运行。
  2. 5 分钟后,我收到 1 级内存警告。
  3. 一分钟后,我收到了 2 级内存警告。
  4. 又一分钟后, Program received signal: “0”.

I checked for leaks using Leaks in Instruments and detected no leaks. However, according to Activity Monitor, my app initially uses 30 MB, and grows in size to over 100mb over time (about 200kb per second!). So, apparently Leaks doesn't detect all memory leaks.

我使用 Leaks in Instruments 检查了泄漏并没有检测到泄漏。但是,根据 Activity Monitor,我的应用程序最初使用 30 MB,随着时间的推移,大小增加到 100 MB 以上(大约每秒 200 kb!)。因此,显然 Leaks 没有检测到所有内存泄漏。

So my question: if Leaks can't detect it, are there any general tips to finding them, and is there a better tool to find these leaks?

所以我的问题是:如果 Leaks 无法检测到它,是否有找到它们的一般提示,是否有更好的工具来查找这些泄漏?

回答by railwayparade

This is how I would approach it,

这就是我将如何处理它,

  1. Name all you classes with a prefix so you can easily see them in Instruments

  2. XCode->Run->Run with performance tool->allocations

  1. 用前缀命名所有类,以便您可以在 Instruments 中轻松查看它们

  2. XCode->运行->使用性能工具运行->分配

Now stop your app and type the name of class you suspect is leaking in the search box. (this is where the prefix is handy). The start instruments again with the "Record" button

现在停止您的应用程序并在搜索框中输入您怀疑泄漏的类的名称。(这是前缀很方便的地方)。使用“记录”按钮再次启动仪器

  1. The living column should go up and down as you allocate objects. Note if it doesn't go down you have a leak.
  1. 当您分配对象时,活动列应该上下移动。请注意,如果它没有下降,则说明存在泄漏。

回答by Krishnan

It is always not necessary that you have leaks in the case you have mentioned. You might have used lot of autoreleased objects in the point of execution time that you mention here. You might be adding a heavily sized objects to a collection object. If that is the case, use a separate Autorelease pool for that case, reduce the memory footprint of the application.

在您提到的情况下,始终没有必要泄漏。您可能在此处提到的执行时间点使用了许多自动释放的对象。您可能正在向集合对象添加大尺寸对象。如果是这种情况,请为这种情况使用单独的 Autorelease 池,以减少应用程序的内存占用。

回答by regality

It's preferred if you can make use tools to find leaks, but if are completely stumped, there is another technique that I use.

如果您可以使用工具来查找泄漏是首选,但如果完全被难住,我会使用另一种技术。

Start with commenting out almost everything and then slowly add back one component at a time until the memory leak occurs again. That way you can zero in on the function or block of code that is creating the problem.

从注释掉几乎所有内容开始,然后一次缓慢地添加一个组件,直到再次发生内存泄漏。这样您就可以将产生问题的函数或代码块归零。