C # managed code, unmanaged code, and recycling mechanism

Source: Internet
Author: User

Online to find the relevant text, found some very good, turn around, easy to view later

Managed Code

Managed code is visual Basic. The code compiled by the net and C # compilers. The compiler compiles the code into intermediate language (IL) instead of the machine code that runs directly on your computer. The intermediate language is encapsulated in a file called an assembly (assembly) that contains all the metadata that describes the classes, methods, and properties that you create, such as security requirements. You can copy this assembly to another server to deploy it. Typically, the action of this copy is the only action in the deployment process.

Managed code runs in the common language runtime (CLR). This runtime provides a variety of services to your running code, and in general, he loads and validates the assembly to ensure the correctness of the intermediate language. When some methods are called, the runtime compiles the specific method into a machine code that is suitable for the local computer to run, and then caches the compiled mechanical code for the next call (this is the instant compilation). As the assembly runs, the runtime continuously provides services such as security, memory management, thread management, and so on. This program is "hosted" in the runtime. Visual Basic. NET and C # can only produce managed code. If you write a program in such a language, the resulting code is managed code. If you prefer, Visual C + +. NET can generate managed code. When you create a project, select the name to be. Managed the project type that starts with. For example. Managed C + + application.

Unmanaged Code

Unmanaged code is code that was created before Visual Studio. NET 2002 was released, such as Visual Basic 6, Visual C + + 6. Worst of all, the code generated by a stale C compiler that still has more than 15 years of history remaining on your hard drive is unmanaged code. Unmanaged code is compiled directly into the machine code of the target computer, which can only be run on the computer that compiles them, or on a computer with the same processor or almost the same processor. Unmanaged code cannot enjoy some of the services provided by the runtime, such as security and memory management. If unmanaged code requires services such as memory management, it must explicitly invoke the interface of the operating system, which typically invokes the APIs provided by the Windows SDK. In recent cases, unmanaged programs get operating system services through COM interfaces. Unlike other programming languages in the Visual Studio platform, Visual C + + can create unmanaged programs. When you create a project and select the type of project whose name begins with Mfc,atl or WIN32, the project produces an unmanaged program.

In summary, unmanaged code is code that runs outside the common language runtime Environment (CLR) and is executed directly by the operating system. Unmanaged code must provide its own garbage collection, type checking, security support, and other services, unlike managed code, which obtains these services from the common language runtime.

difference between the two

For Visual Basic and C #, life is simple, because you have no other choice. When you declare a class in those languages, instances of this class are created in the managed heap, and the garbage collector (GC) helps us manage the recycling of those objects. But in Visual C + +, you have another option. Even if you are creating a managed program, you can decide which classes are managed types and which are unmanaged types.

unmanaged type:

class foo{   private:      int  x;     Public :      Foo (): X (0) {}      Foo (int  XX): X (XX) {}};

Managed types

class bar{  private:      int  x;     Public :      Bar (): X (0) {}      Bar (int  XX): X (XX) {}};

Their only difference is that the _GC keyword is in the definition of class bar. This keyword can make a huge difference to your code.

Managed types can be reclaimed by the garbage collector. They have to be created with the keyword new and will never appear in the stack. So the following line of code is legal:

Foo F; // unmanaged Type

But this line of code is illegal:

Bar b; // The managed type must be created with new

If I create a Foo object in the heap, then I have to be responsible for cleaning up this object:

New Foo (2); //  . . . Delete pf; // Manual Cleanup

The C + + compiler actually uses two heaps, a managed heap, and an unmanaged heap, and then allocates different memory by overloading the new operator to create instances of different types of classes. If I create a bar instance inside the heap, I can ignore it. When no other code is using it, the garbage collector automatically cleans up the class and frees up the resources it consumes. There are constraints on managed types: they cannot implement multiple inheritance, or are inherited versus unmanaged types; they cannot use the Friend keyword for private access, and they cannot implement a copy constructor. So, you might not want to declare your class as a managed type. But that doesn't mean you don't want your code to become managed code. In Visual C + +, you can choose.

the execution process of managed code

Select compiler: To gain the benefits of the common language runtime, you must use one or more language compilers for the runtime, such as Visual Basic, C #, Visual C + +, JScript, or many third-party compilers such as Eiffel, Perl, or COBOL compiler). Because the runtime is a multi-lingual execution environment, it supports a variety of data types and language features. The language compiler that you use first determines the available runtime features, and then uses these features to design your code. The compiler (not the runtime) establishes the syntax that the code must use. If your component must be fully capable of being used by components written in other languages, your component's export type must expose only the language features that are included in the Common Language Specification (CLS).

Compile, translate the source code into Microsoft intermediate Language (MSIL), and generate the required metadata.

When executed, the just-in-time (JIT) compiler translates the MSIL into native code. During this compilation, the code must pass the validation process, which examines the MSIL and metadata to see if the code can be identified as type safe.

Run code: The common language runtime provides a structure that enables execution to occur and the various services that can be used during execution.

C #-Object destruction and garbage collection

Some objects need to be shown to destroy code to free resources, such as open file resources, locks, operating system handles, and unmanaged objects. In. NET, this is called object destruction, which is implemented through the Idisposal interface. Memory management, which is used by objects that are no longer in use, must be recycled at some point; this function, known as garbage collection, is executed by the CLR.

The difference between object destruction and garbage collection is that object destruction is usually explicitly instigated, and garbage collection is completely automatic. In other words, the programmer is responsible for releasing file handles, locks, and operating system resources, while the CLR is responsible for freeing the memory.

C # an alternative to processing destruction--finalizer and its schema.

C # managed code, unmanaged code, and recycling mechanism

Related Article

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.