locked
HEAP and STACK

    Question

  • Hi All,

    I am new to .NET and learning C#,

    I want to know, what HEAP and STACK, where exactly they come into Picture in Application and What things we have to consider with regards to HEAP and STACK.

    Rgds

    Sidheshwar.

    Wednesday, February 28, 2007 4:57 AM

Answers

  • As Qasim indicated, "heap" is a bit different in C# with garbage collection, but how you think about it is simular to other languages...

    When you put a breakpoint somewhere in your code, you can open a stack trace window, which shows which functions you are currently "nested" in.  Each one of these functions during it's compile time figures out how much stack space it must reserve...

    The stack being a section of memory (implemented as a stack) that is reserved for keeping track of function return points and local data, the function will typically reserve enough space to save where the program counter needs to go after the function returns, and space for any local variables it must store while the function's body is being executed.

    So any local variables that are declared in the body of the function end up on the stack.  The key point here is that any local varialbes are deleted at the time the function returns.

    The heap... Any time you use "new", the data you allocate is not placed on the stack, but within a much larger section of memory that is persistant for a much longer time than the life of any one function.  You typically use new for data you want to keep around for a long time... typically longer than the life of one function.  In most language that do not use garbage collection, it's up to the programmer to keep track of the pointers/references to this memory, and to deallocate it when they are done with it.  In C#, the garbage collector will do this for you, typically some time after any reference you have to that data is gone.

    Hope this gives you the "short answer".... stack: temporary storage for the life of a function... heap... longer term storage that can persist longer than the life of one function...

    Hopefully this will be enough of an answer to let you know you really don't have to think too much about it.  Get any C# book of your choosing, and read up on how the garbabe collector works if you want to know more detail, and maybe read up on IDispose.

    Jim Tomasko

     

    Wednesday, February 28, 2007 7:56 AM

All replies

  • Hi
    I assume you are talking about stack and heap in terms of memory allocation. In fact stack and heap are not related to C# in particular. We have the concept of these terms in almost all programming languages. All the dynamic memory is allocated at heap while static memory is allocated at the stack. However stack is used for many other purposes as well. For further details just google these terms and you would find a lot of information.

    Regards
    Qasim
    Wednesday, February 28, 2007 6:13 AM
  • As Qasim indicated, "heap" is a bit different in C# with garbage collection, but how you think about it is simular to other languages...

    When you put a breakpoint somewhere in your code, you can open a stack trace window, which shows which functions you are currently "nested" in.  Each one of these functions during it's compile time figures out how much stack space it must reserve...

    The stack being a section of memory (implemented as a stack) that is reserved for keeping track of function return points and local data, the function will typically reserve enough space to save where the program counter needs to go after the function returns, and space for any local variables it must store while the function's body is being executed.

    So any local variables that are declared in the body of the function end up on the stack.  The key point here is that any local varialbes are deleted at the time the function returns.

    The heap... Any time you use "new", the data you allocate is not placed on the stack, but within a much larger section of memory that is persistant for a much longer time than the life of any one function.  You typically use new for data you want to keep around for a long time... typically longer than the life of one function.  In most language that do not use garbage collection, it's up to the programmer to keep track of the pointers/references to this memory, and to deallocate it when they are done with it.  In C#, the garbage collector will do this for you, typically some time after any reference you have to that data is gone.

    Hope this gives you the "short answer".... stack: temporary storage for the life of a function... heap... longer term storage that can persist longer than the life of one function...

    Hopefully this will be enough of an answer to let you know you really don't have to think too much about it.  Get any C# book of your choosing, and read up on how the garbabe collector works if you want to know more detail, and maybe read up on IDispose.

    Jim Tomasko

     

    Wednesday, February 28, 2007 7:56 AM