Click here to Skip to main content
15,943,362 members
Home / Discussions / C#
   

C#

 
GeneralRe: How memory is allocated in .Net application Pin
Pete O'Hanlon27-Feb-15 7:23
mvePete O'Hanlon27-Feb-15 7:23 
GeneralRe: How memory is allocated in .Net application Pin
Tridip Bhattacharjee1-Mar-15 21:22
professionalTridip Bhattacharjee1-Mar-15 21:22 
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 
See if this helps, click through the various parts

http://blogs.msdn.com/b/ericlippert/archive/2009/04/27/the-stack-is-an-implementation-detail.aspx[^]

1 The OS is asked for memory to be reserved to hold the contents of the object, the object is stored at that address on the heap. As already mentioned, the variable that points to the object is on the stack.

2 Heap

3 Performance. Allocating and deallocating memory is expensive (have a for loop from 1 to 1000000 and in that loop update an int "i = i + 1", do a second loop and in that loop concat a string s = s & "x". Time how long both loops take to execute - the string loop is doing memory allocation, the int loop isn't). With the stack the memory is already allocated, you can put things on the stack and take them off "for free". Every time you put something in the heap you need to request a memory allocation, when you take things off that memory has to eventually be deallocated.

Because the stack is a known fixed size you can't go storing Word documents in it, it would run out (make a function that just calls itself, every call puts an exit reference on the stack, after a second or so you'll get a stack overflow as you will have run out of stack space).

Also things on the stack are "last on first out", they remain in sequential order at known addresses. As the stack is pre-allocated, a "pointer" is held that references the next bit of free space. As you put something on the stack the pointer goes up, you take something off it goes down. If you have, say, an int (10), a string ("ab") and an int (20) on the stack;

Stack add 0 - 10
Stack add 1 - a
Stack add 2 - b
Stack add 3 - 20
Stack add 4 - < stack pointer points here, put something else on and this is where it goes

and then wanted to change the string from "ab" to "abc"...where do you store the "c"? There is no space as your second int is on top of it so to increase the string from "ab" to "abc" would require reordering everything above it. So you can only put things on the stack that don't change in size. An int is always going to be 32 bits for example (depending on os and hardware).

5 Value types, things of known, unchangeable sizes, tend to go on the stack. "objects" of unknown sizes that can grow and shrink go on the heap. Of course value types can end up on the heap too due to things already mentioned, due to boxing, static variables etc, but reference types will never end up on the stack.

Note to everyone: Obviously many things in here are simplified for basic understanding, there's no need to be pedantic Smile | :)
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 
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 

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.