C # 's understanding of managed and unmanaged code

Source: Internet
Author: User

Understanding the CLR (Common Language Runtime) is the prerequisite to understand managed and unmanaged code

The. Net Framework is made up of two separate and interrelated parts: The CLR and the class library, the CLR is the service it provides to us, and the class library is the function it implements.
. NET most of the features----garbage collection, versioning, thread management, etc., all using the services provided by the CLR

When you compile the source code for the. NET framework, the resulting target code is not a machine instruction that the CPU can recognize, but rather a new language called "Microsoft Intermediate Language (MSIL, or code abbreviated as IL)". The CLR provides a real-time compiler,
Used to compile IL code into native machine code. In this way, the CLR can make the code portable because. NET application's source code must be compiled into IL code that can run on any platform that provides CLR services. From the perspective of the CLR,
All languages are equal, as long as there is a compiler that can generate IL code, which ensures interoperability of all languages.

Code that is executed by the common language runtime (CLR) environment, not directly by the operating system. Managed code applications can obtain common language runtime services, such as automatic garbage collection, runtime type checking, and security support.
These services help provide platform-and language-independent, unified managed-code application behavior.

Unmanaged code-Unmanaged

Code that is executed directly by the operating system outside of the common language runtime environment. 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.
Category: C # Fundamentals can be automatically recycled is managed. Unmanaged is the one that cannot be automatically recycled. The system thing is managed.

The heap of. NET is the managed heap. There is no unmanaged heap. The reference target of a reference type is in the heap.

The value of the value type is in the stack.

The so-called system resources. Refers to: Network connection, database connection. File stream. This kind of thing.

Hosting here means being managed by the CLR, which is the heap managed by the CLR. Unmanaged resources need to be released manually, and managed resources are handled by the GC for you.

Recommend a blog post in the garden: understanding of managed and unmanaged resources

I think you can start with the Using keyword, why do we sometimes use

using (SqlConnection conn=new SqlConnection (CONSTR))

{...}

using (Streamstreamwrite=new FileStream ()

{...}

This code writes the database connection to the stream operation.

It is because these resources are unmanaged resources cannot be automatically released by the GC, need to be manually forced to release, how to make the release? The using has this capability to track activities within an unmanaged resource cycle, and once the unmanaged resource life is discovered, the Dispose method is forced to release the memory of the unmanaged resource at that scope. You compare the Java and. NET garbage collection mechanisms and understand some of the basic theories of C # value types, reference types, stacks, managed heaps, and then connect them again to think that you can get the answers you want, and what you want to understand can be completely digested.

C # 's understanding of managed and unmanaged code

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.