内存管理、自动释放等问题,永久堆有时是 250+ iOS 上的知识库

发布于 2024-12-27 09:24:44 字数 893 浏览 4 评论 0原文

我真的对这个很抓狂,看来我在 iOS 应用程序上的内存管理方面遇到了严重的问题。

情况如下:首先我加载表。当用户点击一个单元格时,它会呈现一个复杂的视图。该视图最消耗内存的是它加载 20 多个 500x500 的 UIImage。该视图中还有另外两个选项卡,加载媒体列表(那些 UIImage,但随后位于表格中)和另一个简单的表格。

当我返回到第一个表视图时,显然堆上仍然分配了超过 250 kB。我知道这种观点很复杂,但没有理由保留这么多记忆。好吧,你猜怎么着,当我多次切换到视图时,最终应用程序会耗尽内存并被杀死。

我试图解决的问题:

  • 修复所有分析问题,因此不再有泄漏。
  • 再次检查所有 init 是否释放,尽可能使用 autorelease
  • 使用 Instruments 检查所有内存泄漏 ->泄漏。在运行时间为 6 的情况下,我得到的泄漏不超过 2 或 3 次。
  • 最后,仪器 ->分配,检查堆。这让我很困扰,在两个标记的 heapshot 之间,我得到了 250+ kB 的差异。我已经使用详细视图对其进行了研究。我无法理解它:当它指向我的方法/类之一时,我很确定其中的所有内容都已发布或自动发布。它还指向许多非我的(例如 QuartzCore)方法/类。

另外,我不明白为什么 autorelease 不是自动释放。我的意思是,有时看起来像一个标记为自动释放的对象,释放得太晚了。我自己还没有创建任何 NSAutoreleasePool ,那么是否有可能只有在运行时停止时池才会被耗尽?我如何定期排空游泳池(即使不是我的)。

非常感谢任何帮助。

亲切的问候,

Reinder

将其用于堆检查:

I'm really pulling my hair out on this one, it seems that I'm having severe issues with memory management on an iOS app.

Here's the case: first I load table. When the user taps a cell, it presents a complicated view. The most memory consuming about the view is that it's loading 20+ UIImages of 500x500. There are two other tabs in that view, loading a list of media (those UIImages, but then in a table) and another simple table.

When I return back to the first table view, apparently over 250 kB is still allocated on heap. I know the view is complicated, but there's no reason to keep so much memory alive. And well, guess what, when I do switch to the view a lot, eventually the app runs out of memory and gets killed.

What I tried to solve it:

  • Fix all Analyze issues, so according to that there are no leaks anymore.
  • Check all inits again for releasing, making use of autorelease where possible.
  • Checking all the memory leaks using Instruments -> Leaks. In a runtime of 6, I get not more than 2 or 3 leaks.
  • Last, Instruments -> Allocation, checking the heap. This is what bothers me, between two marked heapshots I get a difference of 250+ kB. I've looked into it, using the detailed views. I can't get my head around it: when it's pointing to one of my methods/classes, I'm pretty sure everything in there is either released or autoreleased. It's also pointing to a lot of not-mine (say QuartzCore) methods/classes.

Also, I don't understand why autorelease is not autoreleasing. I mean, it sometimes looks like an object that is marked for autoreleasing, is released way too late. I haven't created any NSAutoreleasePools myself, so is it possible that the pool is drained only when the runtime stops? How can I periodically drain the pool (even if it's not mine).

Any help is greatly appreciated.

Kind regards,

Reinder

Used this for the heap checking: http://www.friday.com/bbum/2010/10/17/when-is-a-leak-not-a-leak-using-heapshot-analysis-to-find-undesirable-memory-growth/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

揪着可爱 2025-01-03 09:24:44

您是否使用 imageNamed 加载图像 - 此方法会将所有图像缓存在内存中。请尝试使用 initWithContentsOfFile 来代替。

不过要小心; initWithContentsOfFile: 根本不会缓存,因此如果您对同一图像多次使用此方法,那么您应该使用imageNamed:

Are you using imageNamed to load your images - this method will keep all images cached in memory. Try initWithContentsOfFile instead.

Watch out though; initWithContentsOfFile: won't cache at all so if you use this method a lot for the same image then you should be using imageNamed:!

半﹌身腐败 2025-01-03 09:24:44

我认为您可能想首先尝试优化您的设计并阅读高效内存管理指南。更好地了解组件和运行时不仅有助于跟踪内存分配,而且可以更轻松地找到泄漏。

  • 首先,您应该始终使用发布。仅在必要时使用自动释放
  • 确保遵循 UITableView 实现和 UITableViewCells 有效管理的指南(延迟加载、单元格重用等)。
  • 检查是否有保留周期(保留的视图控制器不会被释放)。
  • 跟踪视图控制器和对象的释放
  • 不要将不再需要的东西保留在内存中。
  • 不要加载您现在不需要的东西。

I think you might want to try to optimize your design first and read guides for efficent memory management. A better understaning of the components and the runtime helps more than tracking memory allocations and will make it easier to find the leaks.

  • First you should always use release. Only use autorelease when necessary.
  • Make sure you follow the guidelines for UITableView implementations and efficient management of UITableViewCells (lazy loading, cell reusing etc.).
  • Check if you have retain-cycles (retained view controllers won't be deallocated).
  • Track the deallocation of your view controllers and objects
  • Don't keep stuff in memory you don't need anymore.
  • Don't load stuff you don't need right now.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文