为什么要在 viewDidUnload 和 dealloc 中释放内存
当应用程序即将关闭时,这些方法不是会被调用吗?如果是的话,那内存不就全部清空了吗?
Aren't these methods called when the app is about to be shut down? If so, then won't the memory be all cleared out anyway?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您只有一个持续应用程序持续时间的视图,则目前甚至不会调用 unload 和 dealloc,因此这些方法实际上未使用且不需要。
但是,如果您扩展此应用程序以具有切换使用和不使用的视图和对象,那么在内存不足的情况下,很可能会调用这些方法来降低应用程序的内存占用量,以便应用程序不会因为使用太多内存而被杀死。因此,将它们保留下来(并正确编码它们以释放内部分配的对象和分配的内存)以供将来的代码重用被认为是良好的做法。这就是为什么它们带有各种 Cocoa 模板。
If you only have one view that lasts the duration of the app, then unload and dealloc are currently never even called, so these methods are actually unused and unneeded.
However, if you ever expand this app to have views and objects that get switched in and out of use, then in low memory circumstances these methods may well be called to lower your app's memory footprint so that the app doesn't get killed for using too much memory. So leaving them in (and coding them correctly to release internally allocated objects and malloc'd memory) for future code reuse is considered good practice. That's why they come with the various Cocoa templates.
确实,当应用程序终止时,
viewDidUnload
和dealloc
会被调用,但这些肯定不是唯一的一次。正确实现这些清理方法以及didReceiveMemoryWarning
非常重要。如果您没有在
dealloc
中正确清理,那么您的应用将开始泄漏内存。随着时间的推移,它可能会消耗越来越多的内存,直到被系统终止。同样,如果您的
viewDidUnload
不释放其资源,则可能会泄漏内存。如果多次使用视图,则每次调用都会泄漏。对于 iOS 4,仔细的内存管理比以往任何时候都更加重要,因为如果用户按下 Home 按钮,您的应用程序可能会进入后台。这意味着它的运行时间可能比以往任何时候都长,因此当它重新获得前台时,您将重用相同的视图控制器。如果您的应用程序没有正确释放未使用的内存,它几乎肯定会被系统杀死。
It is true that
viewDidUnload
anddealloc
are called when an app terminates, but these are certainly not the only times. It is very important to correctly implement these cleanup methods, as well asdidReceiveMemoryWarning
.If you don't clean up properly in
dealloc
, then your app will start to leak memory. Over time, it may consume more and more memory, until it gets terminated by the system.Similarly, if your
viewDidUnload
doesn't release its resources, you can be leaking memory. If the view is used multiple times, each invocation will leak.Careful memory management is more important than ever with iOS 4, as your application may end up in the background if the user presses the Home button. This means it may run for longer than ever, and thus you will be reusing the same view controllers when it regains the foreground. If your app doesn't release unused memory properly, it will almost certainly be killed by the system.
viewDidUnload 仅在内存不足的情况下调用。您想要释放在 viewDidLoad 中创建的所有对象。你想将它们配对。您仍然希望释放 dealloc 中的所有内容,因为如果应用程序中从未出现内存不足的情况,则不会调用 viewDidUnload。
viewDidUnload is only called in low memory situations. You want to release all object you create in viewDidLoad. You want to pair them up. You still want to release everything in dealloc, since viewDidUnload will not be called if low memory situations never occur in your app.
请记住,从 NSObject 继承的每个类都有其dealloc,因此当对象的引用计数达到 0 时,将调用其dealloc,这意味着该对象拥有的内存最好被释放。
类似地,viewDidUnload 是每个 UIViewController 都有的一个方法,当不再需要与控制器关联的主视图时调用它,如果您愿意,它不再可见(您可以认为当您从导航堆栈中弹出控制器或在选项卡栏控制器中切换选项卡)。当视图未显示/活动/使用等时,应用程序和iPhone/iPod不拥有视图所拥有的对象是很方便的。
最后,AppDelegate作为一个对象有它自己的dealloc方法,所以也许你会感到困惑可以从这一点出发。
Keep in mind that each class inerithing from NSObject has its
dealloc
and so when the reference count of an object reaches 0 , its dealloc is being called, meaning that the memory owned by that object would better be deallocated.Similarly
viewDidUnload
is a method that each UIViewController has and it is being called when the main view associated to the controller is no more needed, no more visible if you want (you can think of it being called when you a pop the controller from a navigation stack or switch a tab in tabbar controller). It is convenient for the app and the iPhone/iPod not to have the objects owned by the view around when the view is not displayed/active/used etc.Finally the AppDelegate, as an object has its own dealloc method, so maybe your confusion can come from this point.