内存管理、自动释放等问题,永久堆有时是 250+ iOS 上的知识库
我真的对这个很抓狂,看来我在 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+ UIImage
s of 500x500. There are two other tabs in that view, loading a list of media (those UIImage
s, 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
init
s again for releasing, making use ofautorelease
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 NSAutoreleasePool
s 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否使用
imageNamed
加载图像 - 此方法会将所有图像缓存在内存中。请尝试使用initWithContentsOfFile
来代替。不过要小心;
initWithContentsOfFile:
根本不会缓存,因此如果您对同一图像多次使用此方法,那么您应该使用imageNamed:
!Are you using
imageNamed
to load your images - this method will keep all images cached in memory. TryinitWithContentsOfFile
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 usingimageNamed:
!我认为您可能想首先尝试优化您的设计并阅读高效内存管理指南。更好地了解组件和运行时不仅有助于跟踪内存分配,而且可以更轻松地找到泄漏。
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.