iphone - 使用 NSCache : Not clearing or removing memory how I expect

标签 iphone objective-c ios

我正在为我的 iOS 应用程序使用 NSCache。我知道它会处理自己的内存,但我想手动清除它。当我告诉它 removeAllObjects 时,我没有在 Instruments Allocations 中看到我的内存使用量下降。

是否有另一种方式会发生这种情况,或者它不会清空我的想法?

最佳答案

NSCache 将保留您添加到它的对象。

您分配对象、添加到缓存而不释放导致保留计数为 2 是否有可能?将其缩小到一个小片段,用已发布的代码重复行为可能会有所帮助缩小范围。

根据文档:

While a key-value pair is in the cache, the cache maintains a strong reference to it if garbage collection is in effect; in memory-managed code, the cache retains the item.

另外,这里有一段视频展示了如何使用 NSCache 和分配——在那个例子中,他们可以在内存警告下删除所有对象。他们模拟了一个内存警告,它确实减少了内存占用,所以它应该可以工作......

http://speirs.org/blog/2010/6/30/using-instruments-to-learn-about-nscache.html

关于iphone - 使用 NSCache : Not clearing or removing memory how I expect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7814297/

相关文章:

objective-c - 为什么我的原型(prototype)单元格显示为空白?

ios - Xcode 6 刷新 NSUserDefaults

ios - getDocumentsDirectory Swift5 替代品

iphone - xcode 将警告显示为错误

iphone - 如果您的 CSS 布局在 iPhone 移动版 Safari 中困惑,典型的原因是什么?

iphone - 追踪 iOS 内存峰值

iphone - UITableViewCell 中可点击的电话号码

objective-c - 压缩/解压内存中的字符串

ios - 无法让 Stripe 示例应用程序将测试付款发送到 Stripe 帐户

ios - 在较新版本的 Xcode 中编译旧项目时出错