我在 iPhone 上编程已经有一段时间了,在内存管理方面有过糟糕的经历。我想知道下面的方法是否是释放内存的好方法。
int count = [someObject retainCount];
for (int i = 0; i < count; i ++)
{
[someObject release];
}
在我遇到的一些情况下(特别是 UIWebViews
),这种方法是一种绝望的行为。变量的 retainCount 减少到零,这将释放它正在使用的内存。该方法有点脏,但是否存在与之相关的瓶颈?
最佳答案
你不应该依赖于 retainCount
因为 iOS
框架有可能保留对象,
阅读下文 Apple 对 retainCount
的评价。
Important: This method is typically of no value in debugging memory management issues. Because any number of framework objects may have retained an object in order to hold references to it, while at the same time autorelease pools may be holding any number of deferred releases on an object, it is very unlikely that you can get useful information from this method.
要了解您必须遵守的内存管理基本规则,请阅读“Memory Management Rules ”。要诊断内存管理问题,请使用合适的工具:
关于iphone - 这是释放内存的好方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6534594/