Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a processes memory reclaimed when it terminates?

In an application of mine I basically allocate memory in C++ and enqueue it for deallocation in C#. This deallocation runs in the background and is non-deterministic, so in very rare cases it can theoretically happen that the application is exited before all the unmanaged memory is deallocated.

If that is the case, the behaviour is (roughly and very stripped down) the same as if my program was

int main()
{
   Foo* = new Foo();
   return 0;
}

my questions now are

  • Is all the memory a program allocates, but not deallocates automatically reclaimed when the program exits or is it a memory leak that stays until I reboot?
  • If it is automatically reclaimed, what mechanism is responsible for that?

EDIT: This is about Windows only, as some mentioned that this is OS-dependent.

EDIT 2: I am not talking about simply ignoring all memory leaks in my application, but about whether I need to make sure that all memory is properly deallocated right before the application exits.

EDIT 3: This is not about open file handles, destructors and side effects or anything, this is about memory that will be non-deterministically deallocated and very rare cases where the memory is not freed before termination.

like image 653
Thomas Flinkow Avatar asked Nov 20 '17 14:11

Thomas Flinkow


People also ask

Is memory freed when a program exits?

Yes, a "memory leak" is simply memory that a process no longer has a reference to, and thus can no longer free. The OS still keeps track of all the memory allocated to a process, and will free it when that process terminates.

What happens to the allocated memory after a program exits?

The memory is reclaimed by the Operating system once your program exits. The OS doesn't understand that your program leaked memory, it simply allocates memory to the program for running and once the program exits it reclaims that memory.

Do memory leaks persist?

Yes, it gets a contiguous chunk of virtual memory. This virtual memory is backed by physical memory as needed. I imagine once a program completes, all of this would be freed (freed = open for new program to consume).

Does the heap memory gets deallocated after program execution?

Unlike stack memory, heap memory is allocated explicitly by programmers and it won't be deallocated until it is explicitly freed. To allocate heap memory in C++, use the keyword new followed by the constructor of what you want to allocate.


1 Answers

If you care only about memory, you might not need to call delete because the operating system will destroy your entire virtual address space at process termination. Consider reading Operating Systems: Three Easy Pieces (freely downloadable) to understand more about OSes.

But you want to avoid memory leaks, so you'll better clean up properly. On some operating systems, you have tools like valgrind to help detect such leaks (so you don't want them to give spurious warnings).

Read also about RAII. In real life, the constructor (or other methods) of Foo (or indirect data used by it) could consume other resources (database connections, opened files, windows on your screen, daemon processes, remote connections to external services or web servers, robotic arms, etc...) and you want them to be disposed of properly.

Consider using smart pointers.

Liveness of a data structure is a whole program property. I recommend reading the GC handbook, at least to understand the concepts and terminology of garbage collection (which can be viewed as a way to manage resources, and not only raw memory).

(In practice, it depends a lot: if you are coding a million-lines of code program with hundreds of other programmers, you need to be more careful than if you are coding a tiny single-source file program alone; if you code a neural surgery robot it is not the same as a desktop application, etc.... so YMMV)

like image 186
Basile Starynkevitch Avatar answered Oct 10 '22 20:10

Basile Starynkevitch