Where these are stored?

2019-07-14 02:13发布

问题:

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

回答1:

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.



回答2:

This link http://msdn.microsoft.com/en-us/magazine/bb985010.aspx explains garbage collection and some memory management.



回答3:


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



回答4:

You can look at this articles:

C# Heap(ing) Vs Stack(ing) in .NET