我正在为我的 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/