Keeps track what is executing or what's been called
Keeps everything in series of boxes one on top of the next
We can use only the top box content
When we are finished with the top box we throw it away
The stack is self-maintaining
Keeps track of our objects
Holds information
Anything can be accessed at any time
The heap must be cleaned (Garbage Collector)
Unfortunately memory is not infinite
Manage it:
Manually
Automatically (Garbage Collector)
Manages the Heap memory
Every object holds memory in the manged heap
The garbage collector is called periodically
If there are no direct or indirect references the GC frees the memory from the object
GC compacts the heap
public static void Main()
{
int x = 5;
int y = 30;
bool isBigger = x > y;
}
Stack
public int ReturnValue()
{
int x = 3;
int y = x;
y = 4;
return x;
}
x | int = 3
y | int = 4
// The class is Reference type
public class Program
{
static void Main()
{
// The list is Reference type
List<int> list = new List<int>() { 1, 2, 3 };
// The string is Reference type
string a = "Pesho";
}
}
public int ReturnValue()
{
MyInt x = new MyInt();
x.Value = 3;
MyInt y = x;
y.Value = 4;
return x.Value;
}
Stack
Heap
y | POINTER
x | POINTER
Myint
Value | int