Reputation: 14869
I have a doubt about the role of the operating system in regards to a process lifetime right now. I am using Linux.
Suppose that I have an application that creates a set of objects in the heap using new
. During the lifetime of the application I have no need to delete any of those objects except at the exit the application or on an exception before exiting to do the clean-up.
Suppose I don't call delete at the end of the application for all those objects, do usually the OS reclaim/free all the heap allocated to make it available again at the process exits? If the process exit because of an exception or call to return or exit, does this always occurs?
If this is true this means that if I don't call delete
there won't be any impact on the OS or on the other applications running on a machine. Right?
I usually use boost shared pointers
or use delete
but I would like just to clarify this doubt in a OS/Linux context
Kind Regards AFG
Upvotes: 4
Views: 2042
Reputation: 13461
Modern OSes reclaim all resources of a closed process. Not only memory, but also file handles, etc.
Upvotes: 3
Reputation: 146970
You need to be more careful with other resources like file handles, database connections, etc. However, all memory is definitely reclaimed.
Upvotes: 0
Reputation: 134
Fast answer is no damage for OS if program don't call destructors of created objects or not freeing memory or other OS handles. Maximum impact is lost part of files which program has written before exiting.
Therefore Herb Satter in their book write about technique that short-lived applications specially doesn't free memory and don't call destructors for maximum speed of execution.
But better that programs normally handles their used OS resources.
(Sorry for my English)
Upvotes: 0
Reputation: 28892
That is correct. Any leak of memory after the lifetime of a process on a protected mode operating system is as a really nasty bug in the kernel (sometimes processes crash).
Having said that, the simplest way to check for memory leaks is to ensure that the heap has exactly the same number of allocated cells at the end of execution as it has at the beginning of execution. If you do not delete
on exit, you have no way of checking this and will never discover legitimate memory leaks.
Upvotes: 5
Reputation: 4921
No fear, the OS reclaims all the memory. What you need to watch out for is leaving some persistent resources, such as files, in an indeterminate state.
Just FYI my programming language deliberately fails to free memory on exit by default: it's faster that way. However RAII is not permitted. In C++ if you use RAII then you need to take more care.
Upvotes: 2