问题描述
这个问题让我很好奇.像这样的问题总是得到像它通常是安全的,但你不应该假设操作系统会为你做这件事"的答案,这对我来说听起来是个好建议,但我想知道:是否有任何积极开发(已发布)不这样做的操作系统?
This question made me curious. Questions like this always get answers like "It's generally safe but you shouldn't assume that the OS will do this for you", which sounds like good advice to me, but I'm wondering: Are there any actively-developed (released) operating systems that don't do this?
这是在恐龙时代(80 年代)修复的东西吗?
Is this something that was fixed back in the age of dinosaurs (the 80's)?
推荐答案
简短的回答是无".即使是多年前 DOS 上的程序也会在程序终止时释放内存(仅仅是因为程序停止时没有任何东西管理内存).我敢肯定有人可能会看到内核模式代码不一定会在应用程序退出时释放其内存,或者他们可能会引用一些晦涩的嵌入式操作系统......但您可以假设 app-exit 返回您的用户模式代码获得的所有内存.(Windows 3.x 可能会遇到这个问题,具体取决于使用的分配器...)
Short answer is "none". Even a program on DOS years ago would release memory on program termination (simply by the virtue that nothing was managing the memory when the program stopped). I'm sure someone might sight that kernel mode code doesn't necessarily free its memory on app exit or they may cite some obscure embedded os.... but you can assume that app-exit returns all the memory your user mode code acquired. (Windows 3.x might have had this problem depending on which allocator was used...)
你应该释放你的记忆"这个美德的原因是,对于大型软件工程,你应该努力开发使用灵活的组件,因为你永远不知道其他人会如何改变在你离开团队很久之后你的代码.
The reason for the virtue that you "should free your memory" is that for large scale software engineering, you should strive to develop components that are flexible in their use because you never know how someone else is going to change the use of your code long after you've left the team.
这样想.假设您设计了一些设计为单例的类(在应用程序生命周期内仅实例化一次).因此,当您的组件销毁或完成时,您决定不打扰内存清理.在那一刻,这是一个非常好的决定.多年后,在您离开到更绿色的牧场后,其他人可能会出现并决定他们需要在多个地方使用您的类,这样在应用程序的生命周期中就会有很多实例来来去去.您的内存泄漏将成为他们的问题.
Think of it like this. Let's say you design some class that is designed to be a singleton (only instantiated once during the app lifetime). As such, you decide not to bother with memory cleanup when your component destructs or gets finalized. That's a perfectly fine decision for that moment. Years later, after you've left for greener pastures, someone else may come along and decide that they need to use your class in multiple places such that many instances will come and go during the app lifetime. Your memory leak will become their problem.
在我的团队中,我们经常谈到让用户启动的应用程序关闭"只是 exit() 而不进行任何清理.如果我们这样做了,我仍然会强制团队开发能够自行清理的类和组件.
On my team, we've often talked about making the user initiated "close" of the application just be exit() without doing any cleanup. If we ever do this, I would still enforce that the team develop classes and components that properly cleanup after themselves.
这篇关于哪些操作系统不会在程序退出时释放内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!