. NET concepts:. NET programs compile and run

Source: Internet
Author: User

. NET concepts:. NET programs compile and runCategory: C # programming 2012-02-29 15:46 3001 people read reviews (2) favorite reports . NET Compiler language Microsoftassemblyvb.net

The program compiles and runs, in general, basically: the first written program is the source code, then the compiler compiled into the local machine language, and finally run on the local operating system.

To compile a running procedure for legacy code:

. NET compiles and runs like this, first writes the good source code, then compiles to the Microsoft intermediate Language code, when runs the time compiles as the local machine language, simultaneously. NET code runs with a CLR environment to manage programs. For example. NET code compilation Run process:

The following is a detailed description of some of the concepts of the compile runtime.
1. MSIL and JIT
When compiling code created using the. NET framework, instead of creating OS-specific native code immediately, compile the code into Microsoft Intermediate language (Microsoft intermediate
Language,msil) code, these MSIL codes are not dedicated to any one operating system, are not dedicated to any one language, and some are Java-like bytecode. C # and others. NET languages, such as vb.net, are compiled into this language during the compilation phase.
Because the code does not compile the cost machine code directly during the compile phase, more work must be done when the application is executed, which is the task of the Just-in-time (JIT) compiler.
The JIT compiles MSIL into native code dedicated to the structure of an operating system and target machine, so that the operating system can execute the application. The name of the just-in-time here reflects the attributes that MSIL compiles only when needed.
In the past, it was often necessary to compile the code into several applications, each for a specific operating system and CPU structure, which is usually an optimization form (for example, to make the code run faster on AMD chips), But more often it is necessary (for example, running on Windows and Linux operating systems, respectively). It's not necessary now, as the name implies, the JIT compiler uses MSIL code, and the MSIL code is independent of the machine, the operating system, and the CPU. There are several JIT compilers, each of which is used for different structures, and we can always find a suitable compiler to create the required native code. In this way, the user needs to do less work, in fact, the user does not have to consider the system-related details, only need to focus on the functionality of the code is sufficient.

2. Assembly
When you compile an application, the MSIL code that is created is stored in an assembly that includes executable application files that can be run directly on Windows, that do not require additional programs, that have an. exe extension, and that the libraries that are used by other applications (whose extension is. dll).

In addition to containing MSIL, an assembly contains metadata (that is, data information contained in an assembly) and optional resources (other data used by MSIL, such as sound and picture files). In all the files contained in the Assembly, there is a file to save the manifest. (A manifest is a collection of data tables in the metadata section that contains the name of a subset of the files in the assembly, describes the version of the Assembly, the language culture, the publisher, the common export type, and all the files that make up the assembly). It is important to note that an assembly can only be a single program manifest.

Metadata allows assemblies to be fully self-describing, and assemblies can be used without additional information. That is, we no longer need to add the data required by the application to the system registry, so deploying the application is straightforward, just copy the file to a directory on the remote computer.

Of course, you don't have to install all the information you need to run your application in one place. We can write some assemblies to perform the tasks required by multiple applications. At this point, these reusable assemblies are often placed where all applications can access them. In the. NET
Framework, this place is "global Assembly cache", and there are tools to help put the assembly in the buffer cache.

3. Managed code

After compiling the code into MSIL and then compiling it into native code with the JIT compiler, the CLR's task is not fully completed. With. NET
The code written by the framework is managed at execution time, that is, the CLR manages the application by managing memory, handling security, and allowing cross-language debugging. Conversely, applications that are not running under CLR control are unmanaged, and some languages such as C + + can be used to write such applications, such as accessing the operating system's low-level functionality. Using C # primarily writes code that runs in a managed environment that uses the CLR's managed capabilities to let. NET interact with the operating system itself, and, of course, to write code that runs in an unmanaged environment, but requires special labeling. The advantages of managed code are: 1, platform-independent, 2, improve performance, 3, the interoperability of languages.

4. Garbage collection

One of the most important features of managed code is garbage collection (garbage
Collection), which ensures that the memory is completely freed when the application is no longer using some memory. Prior to the introduction of. NET, this work was primarily the responsibility of the programmer, and several simple errors in the code would allocate large chunks of memory to the wrong place, leaving the memory mysteriously missing. This usually means that the speed of the computer slows down, eventually causing the system to crash.

. NET garbage collector frequently checks the computer's memory to remove content that is no longer needed. It may be checked thousands of times in a second or a few seconds, or it can be checked at any time, but it is certainly checked.

5. Application domain

In the previous traditional development we all know that an application corresponds to a process and specifies virtual memory for the process, and the operating system maps the actual physical memory, effectively maintaining the security between processes. But on the other hand, each process consumes a certain amount of system resources, reduces performance, and the communication between processes is more troublesome.
A new concept has been introduced in. NET: the application domain (AppDomain). It can be understood that many application domains can run on the same one. NET process, can reduce the system consumption, while the different domains are isolated from each other, security protection. It is also relatively simple to communicate between different domains within the same process.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

. NET concepts:. NET programs compile and run

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.