Thanks for the eye-opener.
This part of CLR memory management I can understand now, but it still
leaves few things popping-up in my mind
1. What kind of resources we must clean-up on AppDomain unload event. As
you mentioned in another message, clean-up of static variables with
managed type will rely on the finalizer(types'). So, static variables of
managed type we are not concerned about, assuming they are well-written.
If we have unmanaged memory e.g. char *(as you mentioned), when we try
to store something here, we request some memory from the OS memory
manager, it will be in use till the process is running, but when the
process exits, that part of memory is free. What a process is using is
Process Heap. As soon as the process is gone, the process heap is gone.
I understand that there must be some type of resources which may require
this clean-up process, but why memory? (or security implications?)
2. What I understand is, JIT compiler must be inserting code here and
there to allocate everything on the process heap and the process static
data is part of the stub every .Net assembly is bound to have and that
the details would be more clear by analyzing the ngen'ed image(trying,
but finding it close to impossible) rather than IL. AppDomain lifetime
is subset of the process lifetime, and if a resource remains referenced
by some part of the code in AppDomain lifetime, it still has to be freed
when the process lifetime is over?
4. How should we clean-up the resources accrued by static initialization
in a MarshalByRef singleton class, when used in an application with
multiple AppDomains?
(This is a bit too specific, but it seems to be the exception to this
idea, and what if it really comes up some day)