codelyoko373
codelyoko373

Reputation: 141

Why should the allocated memory be released properly?

I always thought that whenever you Initialised a pointer using "new", that assigned memory would always be used for that pointer, even after the application you're programming terminates. This was till I found out that apparently memory is cleared up by the OS once the application has closed which has confused me slightly since if that's the case, then why is memory leaks within games or other applications such a problem if the memory leak is cleared once the application closes?

Upvotes: 2

Views: 89

Answers (1)

Charlie
Charlie

Reputation: 23768

True, all the memory you allocated within your program is going to be released by the OS when you terminate it.

But there are two important factors behind this story.

  1. If your program allocates memory and they are leaked, you are effectively creating areas in the computer's memory those can neither be used by your application nor by another one running parallelly. This is not good if you expect long lifespans for your programs. It might suffocate the entire system if the leak happens within a long loop.

  2. If your program is going to be some kind of a single instance DLL (such as Windows in-proc/out-of-proc COM server), the entire system is in trouble. This is because the DLL will not be unloaded immediately by the OS even after the user exists the program which uses it.

Writing a program is not only the placement of the logic in your code. It is always about managing your resources accurately and efficiently too. Resources are always limited.

Upvotes: 2

Related Questions