4

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

Abruzzo Forte e Gentile
  • 14,423
  • 28
  • 99
  • 173
  • 3
    See [Memory leak in C,C++; forgot to do free,delete ](http://stackoverflow.com/questions/1232262/memory-leak-in-c-c-forgot-to-do-free-delete). As noted, all modern desktop and server OSes will reclaim memory when the process ends. Embedded systems *might* not, and the standard doesn't require it. – Matthew Flaschen Dec 15 '10 at 11:25
  • Also see http://stackoverflow.com/q/4406402/57428 - it's a closely related question. – sharptooth Dec 15 '10 at 11:27
  • 1
    Beware that maybe in the future someone will use your class or your code in another situation and he/she will need to delete the object during the run time. Be kind with a stranger and always clean up (the strange can be you!). – Alessandro Teruzzi Dec 15 '10 at 11:31
  • Hi all. I thank you very much for all of your response. – Abruzzo Forte e Gentile Dec 16 '10 at 10:14

5 Answers5

5

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.

doron
  • 27,972
  • 12
  • 65
  • 103
3

Modern OSes reclaim all resources of a closed process. Not only memory, but also file handles, etc.

Juraj Blaho
  • 13,301
  • 7
  • 50
  • 96
2

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.

Yttrill
  • 4,725
  • 1
  • 20
  • 29
0

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)

nickela
  • 134
  • 1
0

You need to be more careful with other resources like file handles, database connections, etc. However, all memory is definitely reclaimed.

Puppy
  • 144,682
  • 38
  • 256
  • 465