System.AppDomain class Details (i)

Source: Internet
Author: User

The AppDomain is the CLR (Common Language Runtime: Common language Runtime) that can load assembly, create objects, and execute programs.

The AppDomain is the basic mechanism by which the CLR implements code isolation.

Each appdomain can be run individually, stopped, and each AppDomain has its own default exception handling, and an AppDomain running failure does not affect the operation of other AppDomain.

The CLR creates a default AppDomain after it is loaded by the CLR Host (Windows shell or InternetExplorer or SQL Server). The entry point of the program (the main method) is executed in this default AppDomain.

1, AppDomain and process comparison:

The AppDomain is created in a process that can have multiple AppDomain in a process, but an AppDomain can belong to only one process.

 2. AppDomain vs. thread comparison:

With threading, the AppDomain is a static concept, and knowledge limits the bounds of the object; A thread is a dynamic concept that can run in a different AppDomain. Multiple threads can be created within an AppDomain, but they cannot be qualified to execute code within the AppDomain.

The System.Threading.Thread object in the CLR is actually a soft thread, which cannot be recognized by the operating system, and the operating system can recognize hard thread. A soft thread belongs to only one ppdomain, and through the AppDomain is hard thread. When hard thread accesses an AppDomain, an AppDomain produces a soft thread.

The hard thread has thread local storage (TLS), which is used by the CLR to store the current AppDomain reference for the hard thread and the soft thread reference. When a hard thread crosses into another AppDomain, these references in TLS also change.

 3. Comparison between AppDomain and assembly:

    Assembly is the basic deployment unit for a. NET program, which provides the CLR with a recognized type of metadata, and so on. Assembly cannot be executed alone, it must be loaded into the AppDomain, and then the AppDomain creates the objects in the assembly. A assembly can be loaded by multiple AppDomain, and an AppDomain can load multiple assembly. Each AppDomain refers to a type that needs to initialize the corresponding assembly in its own AppDomain. Therefore, each AppDomain maintains a static variable of a class separately.

4, the AppDomain and the object comparison:

    Any object can belong to only one appdomain,appdomain used to isolate objects, and objects between different AppDomain must communicate through proxy (reference type) or clone (value type).

    Reference types need to inherit System.MarshalByRefObject to be Marshal/unmarshal (Proxy).
The value type needs to be set serializable property to be Marshal/unmarshal (Clone).

5. AppDomain and Assembly Code    

What is the relationship between the AppDomain and the source code of the assembly? Each assembly's code is loaded into each appdomain separately?
First, we're going to divide the assembly into 3 categories.
1.mscorlib, which is the assembly to which each. NET program is referenced.
2.GAC, this is a strong-named public assembly that can be referenced by all. NET programs.
3.Assembly not in the GAC, this is an ordinary Assembly, which can not be strongly named and not put into the GAC. Starting the CLR, you can set the Loaderoptimization property when entering entry point: [Loaderoptimization (Loaderoptimization.multidomain]
static void Main ()
{...}    The Loaderoptimization property can set three different enumeration values to set how code is stored and accessed for the three assemblies previously said. Loaderoptimization Enumeration/attribute Each not in GAC
Value expected Domains in ProcessDomain expected to Run ... Code for MSCORLIB Code for assemblies in GAC Code for assemblies
Singledomain One N/A Per-process Per-domain Per-domain
MultiDomain Many Same program Per-process Per-process Per-process
MultiDomainHost Many Different Programs Per-process Per-process Per-domain
1.SingleDomain, because only one AppDomain is started, code is loaded directly into the AppDomain, which is quicker to access static variables. 2.MultiDomain, all assembly codes are process-level, so all AppDomain accesses only one copy of the code.    This greatly reduces the memory consumed by the program, but because the static variables of the assembly are still in each AppDomain, the code access static variables need to be referenced by the AppDomain before being converted, and the speed will be affected.     3.MultiDomainHost, only the GAC code is shared, non-GAC assembly will still be loaded into the AppDomain being used, thus increasing the access speed of static variables and, of course, increasing the memory usage of the program.   Either way, mscorlib is always at the process level, that is, only one copy of the mscorlib code is in memory. Detailed reference:http://www.cnblogs.com/foman/archive/2009/10/18/1585655.html

System.AppDomain class Details (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.