.NET Core 3.0 can uninstall the assembly principle Brief

Because recently asked how to understand .NET Core 3.0 can uninstall the assembly, so I wrote this simple analysis in the group.
Because it was really very little time, this article is simply a list of the relevant code, with official documentation to understand.
In addition, the book ".NET Core underlying principles" is expected to published in November, publisher Compare drag: O.

link

The official can uninstall the assembly documentation as follows:

The IL code in the program after the JIT compiler, are stored in the native code Code Heap LoaderAllocator managed, LoaderAllocator code following address:

Responsible for the distribution of native code is CodeManager, the code at the following address:

GC codes are as follows:

Object header (MethodTable) code is as follows:

AssemblyLoadContext code is as follows:

https://github.com/dotnet/coreclr/blob/release/3.0/src/System.Private.CoreLib/shared/System/Runtime/Loader/AssemblyLoadContext.cs

analysis

In .NET Core, we can not create a new AppDomain (although there are a few default AppDomain), program management meetings by AssemblyLoadContext. Simply put, there is AssemblyLoadContext responsible for managing the dependencies of a set of assemblies, such as assembly A dependent assemblies B, then A and B need to use the same AssemblyLoadContext loaded. Different are associated with each AssemblyLoadContext LoaderAllocator, that is, have a different Code Heap.

.NET Core 3.0 began to allow users to create AssemblyLoadContext unloading, i.e. AssemblyLoadContext recovering resources allocated for the assembly, including the JIT generating native code, PreCode, and other types of metadata, the flow is as follows:

  • Users create AssemblyLoadContext (isCollectible = true)
  • User AssemblyLoadContext loader assembly A
  • User AssemblyLoadContext loader assembly B
  • Assembly method for a user to create instances of the A and B type, and executes the
  • User uninstalls AssemblyLoadContext
  • .NET Core wait for all the assemblies of Examples A and B type have been recovered after the release LoaderAllocator resource allocation management AssemblyLoadContext

Can refer to FIG understood (which is a simplified process flow details given above can see the official documentation link):

When you uninstall AssemblyLoadContext, unlinked to LoaderAllocator the code is as follows:

https://github.com/dotnet/coreclr/blob/release/3.0/src/binder/clrprivbinderassemblyloadcontext.cpp#L276

When GC mark object, while the code associated with the tag LoaderAllocator follows (in gc.cpp inside):

#define go_through_object_cl(mt,o,size,parm,exp)                            \
{                                                                           \
    // 如果对象的 MethodTable 是由可回收的 AssemblyLoadContext 加载的
    if (header(o)->Collectible())                                           \
    {                                                                       \
        // 获取关联的 LoaderAllocator
        uint8_t* class_obj = get_class_object (o);                             \
        uint8_t** parm = &class_obj;                                           \
        // 标记 LoaderAllocator (根据 exp 的具体逻辑而定)
        do {exp} while (false);                                             \
    }                                                                       \
    // 如果对象包含引用类型的成员
    if (header(o)->ContainsPointers())                                      \
    {                                                                       \
        go_through_object_nostart(mt,o,size,parm,exp);                      \
    }                                                                       \
}
// 调用 MethodTable::GetLoaderAllocatorObjectForGC
#define get_class_object(i) GCToEEInterface::GetLoaderAllocatorObjectForGC((Object *)i)

LoaderAllocator be recovered after the release of resources to the relevant code (refer to the official logic before being recycled documentation):

Description stop here. You might wonder why the article did not mention Assembly and DomainAssembly, because they can unload accomplish assembly is not important, resource allocation and deallocation is LoaderAllocator unity through AssemblyLoadContext associated not so much a program can be uninstalled set as it is to uninstall the assembly is loaded context (AssemblyLoadContext).

Guess you like

Origin www.cnblogs.com/zkweb/p/11516062.html