1

I am learning GC on .net. I would like to know, where are my integers, floats or value types, static variable stored, Member of the functions, value types in the function are stored.

Any documents or any weblink you have on this topics, please post it here.

Thank you, Harsha

Harsha
  • 1,861
  • 7
  • 28
  • 56
  • It's really not relevant. This is all handled for you; that's the whole point of automatic memory management. Any handy rule you see about value types being allocated on the stack vs reference types being allocated on the heap is overly simplistic and potentially misleading. – Cody Gray - on strike Feb 23 '11 at 07:50
  • if possible read about heap and stack and how objects are stored, you will get a clear picture how memory is allocated and destructed – kobe Feb 23 '11 at 07:51

2 Answers2

6

I have an article which talks about this a bit, but you should really read various blog posts by Eric Lippert. "The truth about value types" is probably the most important one, along with "The stack is an implementation detail" (part one; part two).

Fundamentally it's more important to understand garbage collection in terms of reachability etc, rather than the implementation details of what goes where in memory. That can be helpful in terms of performance, but you need to keep reminding yourself that it's an implementation detail.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • +1 Thanx for that article (Both your and Eric's). I had the same myth : – Shekhar_Pro Feb 23 '11 at 08:03
  • Thank you Very much for the link. Jon, Any other blogs, books, one should be reading to get good knowledge about .net frame work, Design Patterns. – Harsha Feb 23 '11 at 08:49
  • 2
    @Harsha: Read *all* of Eric Lippert's blog. Eric is extremely wise, and writes very well. – Jon Skeet Feb 23 '11 at 09:01
1

enter image description here


Note: Jon Skeet's Answer is more Correct
Stack memory:

The stack is the section of memory that is allocated for automatic variables within functions.

Data is stored in stack using the Last In First Out (LIFO) method. This means that storage in the memory is allocated and deallocated at only one end of the memory called the top of the stack. Stack is a section of memory and its associated registers that is used for temporary storage of information in which the most recently stored item is the first to be retrieved.

Heap memory

On the other hand, heap is an area of memory used for dynamic memory allocation. Blocks of memory are allocated and freed in this case in an arbitrary order. The pattern of allocation and size of blocks is not known until run time. Heap is usually being used by a program for many different purposes.

The stack is much faster than the heap but also smaller and more expensive.

Example: (Its for C though not C#)

    int x;                        /* static stack storage /
    main() {
       int y;                     / dynamic stack storage /
       char str;                  / dynamic stack storage /
       str = malloc(50);          / allocates 50 bytes of dynamic heap storage /
       size = calcSize(10);       / dynamic heap storage */

Above content Taken from Here

Community
  • 1
  • 1
Shekhar_Pro
  • 18,056
  • 9
  • 55
  • 79
  • 1
    Your blanket statement at the top is extremely dangerous. It suggests that *all* value type values are stored on the stack, which is entirely untrue. You've then given an example from C... – Jon Skeet Feb 23 '11 at 07:59
  • It's not a C example, either. There's no such thing as `void main()`. – Cody Gray - on strike Feb 23 '11 at 08:11