Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the life cycle of a .net application [closed]

Tags:

c#

.net

Having a simple .net .exe application. What is it's life cycle once executed. My understanding is that the following occurs:

> 1. OS loads exe assemblies into memory
> 2. OS checks if it is in fact .net assembly
> 3. mscoree.dll loads, and loads CLR
> 4. CLR takes over and loads external dlls, GC stuff, memory management etc.
> 5. CLR creates app domain where exe assemblies are loaded
> 6. exe is started

Given the above is correct (please feel free to elaborate), I'm interested in the last step, when the CLR loads assemblies.

  1. How many stacks, heaps, threads are created?. Is the thread created and that executes the code within the executable?
  2. What is the size of the initial memory allocated?, who allocates the memory (OS or CLR?)
  3. How does it know how much memory is initially needed?
  4. If more memory is needed when the exe runs, who decides how much and when to allocate this memory?
  5. What happens when you close the exe?, does the CLR run any GC right before unloading the App Domain? (closing exe), or does the OS?
like image 867
ShaneKm Avatar asked Nov 26 '15 19:11

ShaneKm


1 Answers

How many stacks, heaps, threads are created?

In the process, in which you application lives, there may be many threads. However, one of them would be the main thread of execution.

When a thread is created it is allocated a 1-MB stack.

who allocates the memory (OS or CLR?)

As is stated here

The common language runtime's garbage collector manages the allocation and release of memory for an application

This is a significant difference between managed and un-managed programs. If you have programmed in either C or C++, you definitely know that this responsibility belongs to the developer and not to a garbage collector. It's a great power. But with great power comes great responsibility. You are responsible for finding (creating) the needed memory space. Then you allocate there your object and when you don't need it more, you have to release this memory. Any mistake that may done in the above process leads to memory leaks and even to the crash of your program. Let alone the difficulty of troubleshooting a bug like this.

On the other hand, in the world of managed programs (C#, Java, JavaScript, etc.) this responsibility belongs to a piece of the runtime that is called garbage collector. The garbage collector allocates the memory and decides, when it's the appropriate time to kick in and garbage the litters. This from it's own is a great convenience making the developer's life significantly easier. However it's a trade-off. A managed program can't hit the performance of a well structured un-managed program.

What happens when you close the exe?, does the CLR run any GC right before unloading the App Domain? (closing exe), or does the OS?

When you close an executable, one of the things that will be done, before the unloading of the application domain takes place, is a garbage collection, in order of any used resources to be released. Then the application domain would be unloaded. After this the CLR would be detached from the process's memory space and last the process would be killed.

like image 161
Christos Avatar answered Oct 21 '22 04:10

Christos