在iOS中泄露内存 [英] Leaking memory in iOS

查看:89
本文介绍了在iOS中泄露内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行了仪器的'Leaks'工具来测试我的应用程序是否有任何泄漏,并且它向我显示我有一些泄漏。我不是修理泄漏的专家,我想知道是否

I ran the instruments 'Leaks' tool to test if my app has any leaks, and it showed me that I have some leaks. I'm not an expert at fixing leaks, I was wondering if


  1. 我有泄漏,

  2. 我该怎么做才能解决它。

推荐答案

您似乎有泄漏,但看起来不大。您可以单击内存地址旁边的小箭头,它应该会显示一个屏幕,您可以在其中钻取并查看内存的分配位置,这是确定未分配内存的第一步。 (我会启动非 malloc 对象,因为它们通常会更直接地映射到您的代码,并且更容易诊断)。

You would appear to have a leak, but it looks modest. You can click on the little arrows next to the memory address and it should take you a screen in which you can drill in and see where that memory was allocated, which is the first step in figuring out why it wasn't deallocated. (I'd start the non-malloc objects, as more often they map more directly to your code and it's easier to diagnose).

但有时候你会看到像迈克罗宾逊所说的那样适度的泄密,误报。即使它不是误报,也可能来自操作系统本身,而不是代码。因此,我们有时会通过练习强调应用程序(例如,反复运行应用程序中似乎会产生泄漏的部分)来查看泄漏增长的速度,如果有的话。看起来您的泄漏可能会增加到不到1千字节左右,并且不会继续增长,您可能会选择不担心它。 (或者至少一旦你满意自己,你的代码中没有任何东西导致它。)

But sometimes you'll see modest leaks like this which are, as Mike Robinson said, false positives. And even if it's not a false positive, it could be coming from the OS, itself, not your code. So we sometimes go through an exercise of really stressing the app (e.g. repeatedly running through the portion of the app that seemed to generate the leak) to see how rapidly the leak grows, if at all. It looks like your your leak might add up to less than 1 kilobyte or so, and doesn't continue to grow, you might choose to not worry about it. (Or at least once you've satisfied yourself that there's nothing in your code that causes it.)

但就个人而言,我并不关心这些适度的泄漏而不是整体内存使用量的显着增长。它可能只是一个适当的图像缓存,或者它可能是一些废弃内存的标志(泄漏工具不会告诉你)。我会尝试模拟内存警告,看看有多少内存被恢复。您还可以在时间轴上拖动并转到分配视图,您可以查看该内存消耗的帐户。您可能希望确保没有与泄漏工具报告的适度泄漏无关的更深层内存问题。并非所有内存问题都出现在泄漏中:分配增长也可能表明存在问题,而且我有点担心你没有看到你的内存使用量下降到某个稳态水平。

Personally, though, I'm less concerned about these modest leaks than the significant growth in overall memory usage. It might just be an appropriate caching of images, or it might be a sign of some abandoned memory (which leaks tool won't show you). I'd try simulating memory warning and see how much of that memory is recovered. You can also drag across the timeline and go to the allocations view, and you can see what accounts for that memory consumption. You might want to make sure you don't have some deeper memory problem unrelated to the modest leaks reported by "Leaks" tool. Not all memory problems appear in "Leaks": The "Allocations" growth can also indicate problems, and I'd be a little worried that you're not seeing your memory usage drop down to some steady-state level.

Apple分享了一个示例分配图,建议我们注意红色的浪费内存。预热部分不是那么关键,也不是中间级别(只要它不是高),但稳态级别的增长表明存在更严重的内存问题:

Apple shared an example allocation graph, advising us to watch out for the red "wasted" memory. The warmup portion is not so critical, nor is the intermediate level (as long as it's not too high), but the growth of the steady state level is indication of a more serious memory problem:

在您的情况下,我没有看到该应用完全恢复稳定状态,这就是为什么我有点担心。但是我不确定你运用了多少应用程序,或者你是否给它机会恢复到稳定状态。

In your case, I'm not seeing the app return to a steady state at all, which is why I'm a tad concerned. But I'm not sure how much you exercised the app or whether you gave it a chance to return to that steady-state.

如果你看(有点过时了,但仍然相关)WWDC 2013 修复内存问题,它将为您提供帮助用于诊断和解决内存问题的工具和技术。这是上面图表的来源,并对其进行了更详细的描述。请注意,PDF演示文稿很不错,但视频要好得多,因为它包含一些使用乐器的实际演示。 WWDC 2012 iOS应用程序性能:内存也很不错。 (看起来流媒体视频可能有问题,但看起来你仍然可以下载它。)

If you watch (the somewhat dated, yet still relevant) WWDC 2013 Fixing Memory Issues, it will arm you with tools and techniques for diagnosing and resolving memory issues. It is where the above chart came from and describes it in greater detail. Note, the PDF presentation is nice, but the video is much better, as it includes some practical demonstrations for using Instruments. The WWDC 2012 iOS App Performance: Memory is also good. (It looks like there might be problems streaming the videos, but it looks like you can still download it.)

这篇关于在iOS中泄露内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆