Click here to Skip to main content
15,887,683 members
Home / Discussions / C#
   

C#

 
GeneralRe: How memory is allocated in .Net application Pin
manchanx26-Feb-15 4:24
professionalmanchanx26-Feb-15 4:24 
GeneralRe: How memory is allocated in .Net application Pin
Tridip Bhattacharjee26-Feb-15 22:01
professionalTridip Bhattacharjee26-Feb-15 22:01 
GeneralRe: How memory is allocated in .Net application Pin
Pete O'Hanlon26-Feb-15 22:53
mvePete O'Hanlon26-Feb-15 22:53 
GeneralRe: How memory is allocated in .Net application Pin
OriginalGriff26-Feb-15 4:41
mveOriginalGriff26-Feb-15 4:41 
GeneralRe: How memory is allocated in .Net application Pin
Santosh K. Tripathi2-Mar-15 17:07
professionalSantosh K. Tripathi2-Mar-15 17:07 
AnswerRe: How memory is allocated in .Net application Pin
F-ES Sitecore26-Feb-15 3:59
professionalF-ES Sitecore26-Feb-15 3:59 
GeneralRe: How memory is allocated in .Net application Pin
Tridip Bhattacharjee26-Feb-15 4:16
professionalTridip Bhattacharjee26-Feb-15 4:16 
GeneralRe: How memory is allocated in .Net application Pin
F-ES Sitecore26-Feb-15 4:42
professionalF-ES Sitecore26-Feb-15 4:42 
The heap is just a big section of memory that you can allocate if you need, it's a free-for-all where objects are placed wherever there is space. A stack in a small, pre-allocated piece of memory that you use in a specific way.

Imagine a stack of plates. Each time you want to store something you place it on the top of the stack of plates. On top of the existing plates is the only place a new plate can go. To take plates off, you can only take off the top plate.

Think of this code
function A()
{
   int x = 1;
   Person p = new Person();
   B();
   int y = 2;
}

function B()
{
   int x = 100;
}


When "A" is run, 1 is put on the stack for x

Stack address: value at that address

0: 1 (x)
1: 0
3: 0
4: 0
5: 0
6: 0
7: 0
8: 0
9: 0

A new "Person" is created so a memory allocation request is made and the OS gives you memory address 10,000, so your Person data is stored at memory address 10,000 on the heap. "p" is actually the number 10,000, ie the place on the heap the Person can be found, so p goes on the stack

0: 1 (x)
1: 10,000 (p)
3: 0
4: 0
5: 0
6: 0
7: 0
8: 0
9: 0

The code has to call "B", however it needs to know where to come back to after B, so the address in memory of the current line of code is stored on the stack. Let's say that is address 1234

0: 1 (x)
1: 10,000 (p)
3: 1234 (ref to code in function A)
4: 0
5: 0
6: 0
7: 0
8: 0
9: 0

Now stack slots 0, 1 and 3 belong to function "A" and we start running code in "B". "B" defines x as 100 so that goes on the stack.

0: 1 (x)
1: 10,000 (p)
3: 1234 (ref to code in function A)
4: 100 (x)
5: 0
6: 0
7: 0
8: 0
9: 0

So stack spots 0-3 belong to A and 4 belongs to B. B is done so we de-allocate everything it put on the stack

0: 1 (x)
1: 10,000 (p)
3: 1234 (ref to code in function A)
4: 0
5: 0
6: 0
7: 0
8: 0
9: 0

To work out where to go back to we read the top-most value of the stack (1234) and resume executing the code at that address. As we have read the value off the stack we also remove it

0: 1 (x)
1: 10,000 (p)
3: 0
4: 0
5: 0
6: 0
7: 0
8: 0
9: 0

This will take us back to the "int y = 2" line in A. Note that not only are we back in the code for A right where we left off, but all of A's variables are on the stack for us to access. So define y = 2

0: 1 (x)
1: 10,000 (p)
3: 2 (y)
4: 0
5: 0
6: 0
7: 0
8: 0
9: 0

A finishes so all of its variables are taken from the stack, and as "p" was a reference, the object at that reference is disposed and eventually the garbage collection will deallocate its memory, allowing address 10,000 to be freed for use by other things.

0: 0
1: 0
3: 0
4: 0
5: 0
6: 0
7: 0
8: 0
9: 0
GeneralRe: How memory is allocated in .Net application Pin
Richard MacCutchan26-Feb-15 5:09
mveRichard MacCutchan26-Feb-15 5:09 
GeneralRe: How memory is allocated in .Net application Pin
Tridip Bhattacharjee26-Feb-15 21:03
professionalTridip Bhattacharjee26-Feb-15 21:03 
GeneralRe: How memory is allocated in .Net application Pin
Richard MacCutchan26-Feb-15 21:19
mveRichard MacCutchan26-Feb-15 21:19 
GeneralRe: How memory is allocated in .Net application Pin
Santosh K. Tripathi2-Mar-15 17:18
professionalSantosh K. Tripathi2-Mar-15 17:18 
GeneralRe: How memory is allocated in .Net application Pin
Richard MacCutchan26-Feb-15 22:30
mveRichard MacCutchan26-Feb-15 22:30 
QuestionRegarding Network buffer and datareader data fetch c# Pin
Tridip Bhattacharjee26-Feb-15 2:33
professionalTridip Bhattacharjee26-Feb-15 2:33 
GeneralRe: Regarding Network buffer and datareader data fetch c# Pin
PIEBALDconsult26-Feb-15 3:31
mvePIEBALDconsult26-Feb-15 3:31 
QuestionNeed help for auto response of a sms Pin
MA11026-Feb-15 2:03
MA11026-Feb-15 2:03 
AnswerRe: Need help for auto response of a sms Pin
Richard Andrew x6426-Feb-15 9:08
professionalRichard Andrew x6426-Feb-15 9:08 
QuestionNeed various algo asked during interview for dotnet C# Pin
Tridip Bhattacharjee26-Feb-15 0:19
professionalTridip Bhattacharjee26-Feb-15 0:19 
AnswerRe: Need various algo asked during interview for dotnet C# Pin
Pete O'Hanlon26-Feb-15 0:56
mvePete O'Hanlon26-Feb-15 0:56 
GeneralRe: Need various algo asked during interview for dotnet C# Pin
Tridip Bhattacharjee26-Feb-15 2:25
professionalTridip Bhattacharjee26-Feb-15 2:25 
GeneralRe: Need various algo asked during interview for dotnet C# Pin
Richard Givis24-Mar-15 11:03
Richard Givis24-Mar-15 11:03 
AnswerRe: Need various algo asked during interview for dotnet C# Pin
OriginalGriff26-Feb-15 0:57
mveOriginalGriff26-Feb-15 0:57 
GeneralRe: Need various algo asked during interview for dotnet C# Pin
Tridip Bhattacharjee26-Feb-15 2:25
professionalTridip Bhattacharjee26-Feb-15 2:25 
GeneralRe: Need various algo asked during interview for dotnet C# Pin
OriginalGriff26-Feb-15 2:37
mveOriginalGriff26-Feb-15 2:37 
GeneralRe: Need various algo asked during interview for dotnet C# Pin
jschell26-Feb-15 10:05
jschell26-Feb-15 10:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.