在内存不足的情况下release不会释放内存
我正在尝试按照Apple的建议来处理低内存警告(在WWDC 2009视频的Session 416中找到),通过释放我的dataController对象(在我的应用程序委托中引用)来释放资源,该对象包含大量字符串从 plist 中读取:
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
[_dataController release];
_dataController = nil;
NSLog([NSString stringWithFormat:@"applicationDidReceiveMemoryWarning bottom... retain count:%i", [_dataController retainCount]]);
}
但是当我在 Instruments 中运行 ObjectAlloc 并模拟低内存条件时,我没有看到应用程序使用的内存减少,即使我看到 NSLog 语句写出并且保留计数为零物体。我确实将对应用程序委托的引用传递给一些视图控制器。但上面的代码释放了对 _dataController 对象(包含 plist 数据)的引用,因此我希望释放内存。
任何帮助将不胜感激。
I am trying to follow the Apple's recommendation to handle low-memory warnings (found in Session 416 of WWDC 2009 videos) by freeing up resources used by freeing up my dataController object (referenced in my app delegate) that contains a large number of strings for read from a plist:
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
[_dataController release];
_dataController = nil;
NSLog([NSString stringWithFormat:@"applicationDidReceiveMemoryWarning bottom... retain count:%i", [_dataController retainCount]]);
}
But when I run ObjectAlloc within Instruments and simulate a Low-Memory Condition, I don't see a decrease in the memory used by my app even though I see the NSLog statements written out and the retain count is zero for the object. I do pass references to the app delegate around to some of the view controllers. But the code above releases the reference to the _dataController object (containing the plist data) so I would expect the memory to be freed.
Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确定应用程序委托是
_dataController
的唯一所有者吗?-release
仅减少引用计数,除非引用计数降至零,否则不会释放对象。如果
_dataController
也被其他对象拥有,请向他们发送消息以释放它。Are you sure that app delegate is the only owner of
_dataController
?-release
only decreases the ref count, it won't deallocate the object unless the ref count drops to zero.If
_dataController
is owned by other objects too, send a message to them to release it.我正在仔细检查这一点。感谢您的投入!我确实重新阅读了 Apple 的内存管理文档,并且确实在 DataController 的 dealloc 方法中放置了一条 NSLog 语句,并且正在调用它。我还把_dataController的释放前的保留计数写出来并设置为nil。保留计数为 1。
这让我回想起为什么我没有看到内存使用量显着减少。我认为我需要更好地了解 Instruments 中的 ObjectAlloc 显示以及我的应用程序中最大内存分配发生的位置。在寻找这方面的帮助后,我对尝试从仪器中确定发生这种情况的位置感到沮丧。我可以看到“所有分配”有 3.54 MB,Malloc 32.00 KB 有 608 MB。如果我深入研究 Malloc,我只会看到负责任的调用者是像 png_malloc 和 inflateEnd 这样的框架调用。我正在我的代码中寻找负责 Malloc 的调用,但我没有看到这一点。所有这些都说明我想知道我是否正在释放一个或多个对象,这些对象确实会对低内存条件下使用的内存量产生重大影响。我想我需要一个深入的仪器教程。 Apple 帮助文档还可以,但带有代码的示例会更有帮助。
I am double-checking that. Thanks for the input! I did reread the memory management docs from Apple and I did put an NSLog statement in the dealloc method of my DataController and it is being called. I also put wrote out the retain count before the release and setting to nil of _dataController. The retain count is 1.
So this drives me back to why I am not seeing a significant decrease in memory usage. I think I need to understand the ObjectAlloc display in Instruments better and where the largest allocations of memory are taking place in my app. After searching for help in this area, I am frustrated with trying to determine from Instruments where this occurs. I can see that there is 3.54 MB for "All Allocations" and 608 MB for Malloc 32.00 KB. If I drill down on Malloc, I only see the Responsible Caller as being framework calls like png_malloc and inflateEnd. I am looking for calls within my code that is responsible for the Malloc but I don't see that. All this to say that I wonder if I am releasing the object or objects that will really make a significant difference in the amount of memory used for the low-memory condition. I think I need an in-depth tutorial for Instruments. The Apple help docs are okay but an example with code would be more helpful.