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?
Is this something that was fixed back in the age of dinosaurs (the 80’s)?
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.
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.