在iOS上,内存管理,自动释放,永久堆的问题有时会超过250 kb [英] Problems with memory management, autorelease, permanent heap is sometimes 250+ kb on iOS
问题描述
我真的把头发拉到这个上面,似乎我在iOS应用程序上遇到了严重的内存管理问题。
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.
以下是这种情况:首先我加载表。当用户点击一个单元格时,它会呈现一个复杂的视图。关于视图的最大内存消耗是,它正在加载20+ UIImage
的500x500。该视图中还有另外两个选项卡,加载媒体列表(那些 UIImage
s,但随后在表中)和另一个简单表格。
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.
当我返回到第一个表视图时,仍然在堆上分配了超过250 kB。我知道这个观点很复杂,但是没有理由保持这么多记忆。好吧,猜猜看,当我切换到视图时,最终应用程序耗尽内存并被杀死。
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.
我试图解决的问题:
- 修复所有分析问题,因此根据没有泄漏。
- 全部检查
init
s再次发布,尽可能使用autorelease
。 - 检查所有内存泄漏使用仪器 - >泄漏。在6的运行时间里,我得到的泄漏不超过2或3次。
- 最后,仪器 - >分配,检查堆。这让我感到困扰,在两个标记的快照之间我得到250+ kB的差异。我已经使用详细的视图调查了它。我无法理解它:当它指向我的一个方法/类时,我很确定那里的所有内容都已发布或自动释放。它也指向很多不是我的(比如
QuartzCore
)方法/类。
- 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.
另外,我不明白为什么 autorelease
不会自动释放。我的意思是,它有时看起来像一个被标记为自动释放的对象,被释放的方式为时已晚。我自己没有创建任何 NSAutoreleasePool
,所以有可能只在运行时停止时才将池耗尽?如何定期排水池(即使它不是我的)。
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).
非常感谢任何帮助。
亲切的问候,
Reinder
推荐答案
我认为您可能希望首先尝试优化设计并阅读有效的内存管理指南。更好地理解组件和运行时不仅有助于跟踪内存分配,还可以更容易地找到泄漏。
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.
- 首先你应该始终使用发布。只在必要时才使用 autorelease 。
- 确保遵循UITableView实施指南和UITableViewCells的有效管理(延迟加载,重复使用单元格等)。
- 检查是否有保留周期(保留的视图控制器不会被释放)。
- 跟踪视图控制器和对象的重新分配
- 不要把东西留在你不再需要的内存中。
- 不要加载你现在不需要的东西。
- 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.
这篇关于在iOS上,内存管理,自动释放,永久堆的问题有时会超过250 kb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!