Primitive type)
----------------------------------
Type directly supported by the compiler.
Sbyte/byte/short/ushort/int/uint/long/ulong
Char/float/double/bool/decimal/object/string
Value type)
------------------------------
Value Type instances are allocated to stacks. Value Type variables themselves include all fields of the instance. value types are not controlled by the garbage collector and are automatically released after they leave the scope. All value types are inherited from ValueType. The value type is transmitted by value replication in the default state (unboxed.
Sbyte/byte/short/ushort/int/uint/long/ulong/char/float/double/bool/decimal/enumeration (enum)/Structure (struct)
Reference type)
------------------------------------
The reference types are all or indirectly inherited from the Object. The reference Object memory must be allocated in the managed heap. The reference type variable contains the memory address of the Object in the managed heap. Each reference type object instance contains some additional members, which must be recycled by the garbage collector to release the memory. Note that the reference itself is allocated in the stack. When a reference type object is passed, it only copies the reference (memory address ).
Reference Object/Array (String)/Array/boxed Value Type
Difference between reference type and Value Type
------------
Struct directly inherits from System. ValueType, while enumeration directly inherits from System. Enum, and Enum directly inherits from System. ValueType.
The following example shows their differences:
First, define the class and struct:
Class SomeRef {public Int32 x ;}
Struct SomeVal {public Int32 x ;}
1 SomeRef r1 = new SomeRef (); // allocate to heap
2 SomeVal v1 = new SomeVal (); // allocate to stack
3 r1.x = 5; // modify the data in the referenced heap Space
4 v1.x = 5; // values are directly assigned to the stack.
5 Console. WriteLine (r1.x); // "5"
6 Console. WriteLine (v1.x); // "5"
7 SomeRef r2 = r1; // only copies the pointer to r2.
8 SomeVal v2 = v1; // allocate space on the stack and copy the variable content
9 r1.x = 8; // Changes the content that r1 points to (or r2 points)
10 v1.x = 9; // only v1 content is modified. v2 content is not affected.
11 Console. WriteLine (r1.x); // "8"
12 Console. WriteLine (r2.x); // "8"
13 Console. WriteLine (v1.x); // "9"
14 Console. WriteLine (v2.x); // "5"
You can see the memory allocation at a glance.
Packing
-------------
Packing: Convert the value type to the reference type. When we pass the value type parameter to a method that needs to reference the type parameter, the packing operation is automatically performed.
Steps:
1. allocate memory from the managed heap, including adding method table pointers and SyncBlockIndex.
2. Copy the value type field to the memory.
3. Return the reference of this address.
Unpack
------------
Unbox: Get the pointer to the value type part of the object. After unpacking, the field copy operation is generally performed. The two operations add up to the mutual inversion between packing and packing.
Steps:
1. If the reference is null, NullReferenceException is thrown.
2. If the target is not a boxed value type, an InvalidCastException is thrown.
3. Return a reference to the value type Part Of The boxed object instead of creating a value type instance in the stack. Therefore, GC collection is required to release the instance.
Stack)
------------
Located in the general random-access memory area, the processor can directly access it through the stack pointer. The pointer moves down to create a new bucket and releases it up. It is the fastest and most efficient memory allocation method second to registers (registers. It is generally used to store the value types and references of known sizes. The data allocated in the stack is automatically released after the scope is exceeded.
Heap)
------------
This is a multi-purpose memory pool (general-purpose pool of memory ). The advantage of heap is that when memory is allocated, the compiler does not need to know how much space to allocate and the data lifetime. Objects allocated in the heap can be released only after the garbage collection period is recycled. Therefore, the efficiency is lower than that of the stack.
From SamWang