The. NET type (Types) of those things

Source: Internet
Author: User

Introduction

You are. NET Engineer? That. Types in NETFramework do you know that there are three major categories ? (in addition to reference types and value types, are there?) )

Reference type must be on "heap", value type must be on "stack"?

How much do you know about the layout details of the reference type in memory?

This article will be one by one of these questions

Types classifications in the. Net Framework

C # type categorization. Shadowed are the built-in type keywords of C #.

In addition to the object and string( System.Object and System.String aliases , respectively), The other shadows are simple value types.

The following is an excerpt from the C # Language Specification 5.0 –> 4. Type (page:77)

The types of the C # language are divided into two main classes: value types (values type) and reference types (reference type).

The third type is a pointer, which can only be used in unsafe code.

How are reference types and value types allocated in memory?

This piece we will explain through a small piece of code, before the explanation let us review

Differences in memory handling during the assignment of reference types and value types:
    • Assign a value type a (defined as int a=80;) to another value type B (int b;), i.e. (b=a;), a copy of the value of a to a, such as;

    • A reference type a (defined as employee A=new employee ()) is assigned to another reference type B (Employee B;), i.e. (b=a;), a copy of the address (reference) of a is copied to a, that is, they point to the same address;

To start with the code, first look at the following code:
Form myForm = new form (); Size S = new size (+, +);          struct = value Typefont f = new Font ("Arial", ten);        class = Reference Typemyform.size = S;myform.font = f;

Note that the font of size and Myform.font in code myform.size is not a type (property) of the form type (class type, which represents a type).

In. NETFramework such use is extremely common, beginners do not confuse the two.

To explain the code before you put forward the knowledge point:
    • Size is a struct type and is, of course, a value type (ValueType)
    • Font is class type, which is of course reference type (referencetype)

The memory allocation for this piece of code above:

It's clear to see

    • The size type s is assigned to the stack, while the front type F and myForm of the form type are allocated on the heap.
    • and the Font property of MyForm refers to the font type F.
    • The Size property of MyForm has its own value (width and height), which is a copy of size type s .

Here we see more clearly the difference between value types and reference types during value assignment

We can modify the font style in MyForm by modifying the value of the font type F, but we cannot modify the size of the myfrom by modifying S.

Object memory layout infrastructure for reference types

The structure shown above is derived from the analysis of the source code:

    1. First objectheader(the thread in the AppDomain in which it is located has the object locked by calling Monitor.Enter)
    2. Next is the method Table pointer , which points to the managed type declared (defined) in the AppDomain, and if the assembly is loaded into the AppDomain neutral, then the method of the type instance in all the AppDomain The table pointers are the same. The underlying building block of the CLR type system is visible in managed code. (Typehandle.value is a IntPtr)
    3. Finally, this is the value of the instance of the type.

The address of this instance object of the CLR object is also subject to change when garbage collection occurs. (see the compression process in GC for details)

\sscli20\clr\src\vm\object.h

The generational GC requires that every object is at least, bytes//in size.   #define Min_object_size     (2*sizeof (byte*) + sizeof (Objheader))

A. NET object has basically the this layout:

Class object{  protected:    methodtable*    m_pmethtab;};
Class objheader{  Private:    //!!! Notice:m_syncblockvalue *must* is the last field in Objheader.    DWORD  M_syncblockvalue;      The Index and the Bits};
Platform Minimum instance size (bytes)
x86 bytes = 2*4+4
x64 bytes = 2*8+8
for the convenience of people to view the source code, here is provided a source index table sscli File Index
Item SSCLI Path
AppDomain \sscli\clr\src\vm\appdomain.hpp
Appdomainstringliteralmap \sscli\clr\src\vm\stringliteralmap.h
Basedomain \sscli\clr\src\vm\appdomain.hpp
ClassLoader \sscli\clr\src\vm\clsload.hpp
EEClass \sscli\clr\src\vm\class.h
Fielddescs \sscli\clr\src\vm\field.h
Gcheap \sscli\clr\src\vm\gc.h
Globalstringliteralmap \sscli\clr\src\vm\stringliteralmap.h
Handletable \sscli\clr\src\vm\handletable.h
Interfacevtablemapmgr \sscli\clr\src\vm\appdomain.hpp
Large Object Heap \sscli\clr\src\vm\gc.h
LayoutKind \sscli\clr\src\bcl\system\runtime\interopservices\layoutkind.cs
Loaderheaps \sscli\clr\src\inc\utilcode.h
Methoddescs \sscli\clr\src\vm\method.hpp
Methodtables \sscli\clr\src\vm\class.h
ObjectRef \sscli\clr\src\vm\typehandle.h
SecurityContext \sscli\clr\src\vm\security.h
SecurityDescriptor \sscli\clr\src\vm\security.h
Shareddomain \sscli\clr\src\vm\appdomain.hpp
StructLayoutAttribute \sscli\clr\src\bcl\system\runtime\interopservices\attributes.cs
Synctableentry \sscli\clr\src\vm\syncblk.h
System namespace \sscli\clr\src\bcl\system
Systemdomain \sscli\clr\src\vm\appdomain.hpp
Typehandle \sscli\clr\src\vm\typehandle.h

Object memory layout of reference type representative structure

Normal Object

Arrays Object-Array

String Object

Reference

The Truth about. NET Objects and sharing them between AppDomains

Six important. NET Concepts:stack, heap, value types, reference types, boxing, and unboxing

Shared Source Common Language Infrastructure

[Translate Classic article] inside the. NET framework to see how the CLR creates a runtime object

. NET objects ' memory layout

Managed heap and garbage collection

The. NET type (Types) of those things

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.