The .Net Framework comes with a form of automatic memory management called, garbage collecting. Basically what it does is reclaims memory that isn't being used anymore.
https://en.wikipedia.org/wiki/Garbage_co...science%29
What does that mean?
Well say I declare and initialize an integer.
Some where down the road, X gets changed.
Now we have the value 10 just floating in memory some where being unused. Well not just anywhere, it's moved to the dynamic memory allocation also known as the heap where it's later collected by the memory management(Garbage collector).
Since that was an integer, value type - it changed the value directly in the stack because we know an int is ALWAYS gonna be 4 bytes.
Now lets say we have a reference type, a value type that can be dynamic.
So let's say for example we have a string.
So now, "Bon" is stored in the heap and there's a pointer in the stack pointing to its location. So again, some where down the road we change the string.
The new string will be allocated in the heap and "Bon" will become unused without a reference and eventually, get cleaned up. However, the pointer located in the stack will remain the same, untouched.
What if we assign a null value?
Well simply, str remains in the stack and doesn't reference anything.
Alright let me write up a diagram.
Before: int x = 10, string str = "Bon"
[Stack]
x 10 // Note how 10 is directly in the stack
str - // pointer referencing a location in the heap.
[Heap/Dynamic Memory Allocation]
nothing here.
-"Bon"
Now we change our variable values - x = 123, str = "ChangedString"
[Stack]
x 123 //directly changed in the stack
str -
[Heap/Dynamic Memory Allocation]
10 // unused will be cleaned up by the garbage collector
"Bon"// unused will be cleaned up by the garbage collector
-"ChangedString"
I could have botched a few things, I wrote this almost completely from memory. ahh get it?
https://en.wikipedia.org/wiki/Garbage_co...science%29
What does that mean?
Well say I declare and initialize an integer.
Code:
int x = 10;
Some where down the road, X gets changed.
Code:
x = 123;
Now we have the value 10 just floating in memory some where being unused. Well not just anywhere, it's moved to the dynamic memory allocation also known as the heap where it's later collected by the memory management(Garbage collector).
Since that was an integer, value type - it changed the value directly in the stack because we know an int is ALWAYS gonna be 4 bytes.
Now lets say we have a reference type, a value type that can be dynamic.
So let's say for example we have a string.
Code:
string str = "Bon"
So now, "Bon" is stored in the heap and there's a pointer in the stack pointing to its location. So again, some where down the road we change the string.
Code:
str = "ChangedString"
The new string will be allocated in the heap and "Bon" will become unused without a reference and eventually, get cleaned up. However, the pointer located in the stack will remain the same, untouched.
What if we assign a null value?
Well simply, str remains in the stack and doesn't reference anything.
Alright let me write up a diagram.
Before: int x = 10, string str = "Bon"
[Stack]
x 10 // Note how 10 is directly in the stack
str - // pointer referencing a location in the heap.
[Heap/Dynamic Memory Allocation]
nothing here.
-"Bon"
Now we change our variable values - x = 123, str = "ChangedString"
[Stack]
x 123 //directly changed in the stack
str -
[Heap/Dynamic Memory Allocation]
10 // unused will be cleaned up by the garbage collector
"Bon"// unused will be cleaned up by the garbage collector
-"ChangedString"
I could have botched a few things, I wrote this almost completely from memory. ahh get it?
This account is currently banned
Ban reason: Learn from your mistakes.
Ban reason: Learn from your mistakes.