Value types and Reference types

  • The value type stores data directly in Stack memory

  • Reference types only store addresses in the Stack, while variable values ​​are located elsewhere (Heap).

Value Type

A reference type that stores its contents in allocated memory is Stack. When we create a variable of value type, a memory area will be allocated to store the value of the variable directly.

If you assign it to another variable, the value will be copied directly and both variables will work independently.

Reference data types in .NET:

  • Integer types

  • Real number type

  • Logical type bool

  • Character type char

  • Structure type

  • Enum

Reference type

Reference types are used to store a reference value (memory address) of an object but do not store the object itself.

Because the reference type only stores the address of the variable's memory instead of the value of the variable, when assigning a reference variable to another variable, it does not copy the data, it only copies the reference address.

So both variables will refer to the same address on the Heap memory.

This means that when a reference variable is deprecated, it will be marked for the Garbage collection.

Reference data types:

  • Classs

  • Object

  • Array

  • Indexer

  • Interface

Stack and Heap Memory

Stack is statically allocated memory and Heap is dynamically allocated memory, both are stored on the computer's RAM.

You can use Stack if you know for sure the size of the data to be stored before compiling the program, it cannot be too large.

You can use the heap if you don't know exactly how much data you will need at runtime or if you need to allocate a lot of data.

In a multi-thread application, each thread will have its own stack but they will share heap memory. The stack is separate for each thread and the heap is shared across the entire application.

Comparison between Stack and Heap

Stack

Heap

Memory is managed automatically

Memory is managed manually

Small size

Large size

Easy and fast access, easy cache

Difficult to cache because it is scattered in memory

Inelastic, allocated memory cannot be changed

Flexible, allocated memory can be changed

Limited to thread scope

Get access to the entire application

The operating system allocates the stack when the thread is created

The operating system is called by the language at application runtime to allocate heap for the application.

Compare static allocation and dynamic allocation

Static allocation

Issuance of activation

The size must be known at compile time

Variable size is unknown at compile time

Done at compile time

Executed at runtime

Assigned to stack

Assign to heap

FILO (First – in, last- out)

Straggling

In short

We have 2 data types: value and reference:

  1. Values ​​are small, fixed-size data types whose values ​​are stored directly on Stack memory.

  2. Reference is a type that only stores addresses on the stack and values ​​​​stores on the heap.

  3. Both types of memory are stored in RAM, it's just a different area. One side is static allocation, the other side is dynamic allocation.

Why must it be divided?

Because with large data types, such as objects, it is impossible to know its size at compile time, so it is not stored on the stack but only stores the address of the Heap, because the heap is dynamically allocated memory.

For example: We read a Harry Porter book, instead of saying I read Harry Porter book 1 , I have to say I read …. from the first sentence to the last sentence... do you know? Instead of having to state the entire content of the very long book, we just need to refer to the name of the book, which is the memory cell address pointing to the book's content. As for the book's content, that person will read it for himself.

As for short sentences, we can always say them in conversation. For example: Have you eaten rice yet? That sentence is not in any book or it is very short so it can be said in conversation.

That's the type of greed.

Last updated