You are on page 1of 21

C# Memory management

Basharat Hussain

14/10/2020

https://www.c-sharpcorner.com/article/C-Sharp-heaping-vs-stacking-in-net-part-i/
Stack
• Think of the Stack as a series of boxes stacked
one on top of the next.

• The Stack is more or less responsible for keeping


track of what's executing in our code

• We keep track of what's going on in our


application by stacking another box on top every
time we call a method.
Stack
• When we're done with the top box (the method is
done executing) we throw it away and proceed to
use the stuff in the previous box on the top of the
stack.

• We pop in top-down sequence


Heap
• The Heap is more or less responsible for keeping
track of our objects.

• Everything is like stack except top-down pop


sequence (in fact there is no sequence)

• Purpose to hold objects

• Anything in a Heap can be accessed at any time


How to clean Stack and Heap
• The Stack is self-maintaining, meaning that it basically takes
care of its own memory management.  When the top box is
no longer used, it's thrown out. Job finished.

• The Heap, on the other hand, has to worry/think about


Garbage collection (GC) - which deals with how to keep the
Heap clean.
What goes on the Stack and Heap?
• There are four major types to think about:
•  Value Types,
• Reference Types,
• Pointers, and
• Instructions
1. Value Types
• Primitive data types of type
System.ValueType
• int
• bool • long
• byte • sbyte
• char • short
• decimal • struct
• double • uint
• enum
• ulong
• float
• ushort
2. Reference Types
• Complex data types of type System.Object

• class
• interface
• delegate
• object
• string
3. Pointers
• A Reference is often referred to as a Pointer.
• We don't explicitly use Pointers, they are managed by the
Common Language Runtime (CLR).
• A Pointer (or Reference) is different than a Reference Type in
that when we say something is a Reference Type is a means
we access it through a Pointer. 

• Student s = new Student();


• var a = s;
Pointer
•  A Pointer is a chunk of space in memory that points to
another space in memory. 
• A Pointer takes up space just like any other thing that we're
putting in the Stack and Heap and its value is either a
memory address or null. 

• See – pointer can be on heap


• Pointer can be on stack…
• Always pointing to a reference type.
4. Instructions
• Let’s discuss it later …
Q: What goes where?
• We have to discuss two rules:
1. A Reference Type always goes on the Heap.
2. Value Types and Pointers always go where they were declared. 

• Point #1 - clear
• Point #2 – not clear – let’s explain it.
Method and calling sequence
• Remember stack is sequential.
• During a method call – start executing a method (JIT compiler) –

1. Push method’s parameters on the stack.


2. Method itself goes to the type's method table; a JIT compilation is
performed if this is the first time we are hitting the method.
3. As the method executes, we need some memory for every (value
type) variable in body allocated on the stack.
4. The method finishes execution and our result are returned.
5. All memory allocated on the stack is cleaned up by moving a pointer
to the available memory address (before start of this method)
Method AddFive is not allocated on stack – just to show for illustration
Five steps of previous slide are presented.
Value type within a method -> stack
• In this example, our "result" variable is placed on the stack. 

• As a matter of fact, every time a Value Type is declared


within the body of a method, it will be placed on the stack.
Remember the rule, Value Types
always go where they were declared?

• Value Types are also sometimes placed on the Heap.

• if a Value Type is declared outside of a method, but inside a


Reference Type, it will be placed within the Reference Type
on the Heap.
Value type on heap
how does it really affect me?
• Why should we care as a student?

• Yes

• Rule:
• When we are using Reference Types, we're dealing with Pointers to
the type, not the thing itself. 
• When we're using Value Types, we're using the thing itself.
Example 1 – value type
• We'll get the value of x?

• 3 - right
Example 2 – Reference type

• We'll get the value of


x.MyValue?

• 4 - right
• END

You might also like