Memory Management for Class Instance

I have doubt on Memory Management in .Net. As per my understanding the memory is allocated and regained as below :

  • For all value types, framework will allocate memory in the stack.

  • For all reference types, the memory will be allocated in the heap, which will be later flushed out or managed by the GC.

  • For the memory allocated in the stack, the memory will be cleared in the LIFO method, when execution is completed.
  • My doubt is on the below scenario :

    Class MathLibrary{
    
    int number1;
    int number2;
    
    public int computesum(){
        return number1 + number2;
    }
    

    }

    MathLibrary maths = new MathLibrary(); // Class instance
    

    How does the memory allocation happens here, I have an understanding that a reference for this object(maths) will be stored in the stack and the actual memory will be allocated in the heap. If then, where would be the memory allocated for the value types (the two integer variables) and for the method definition.


    For all value types, framework will allocate memory in the stack.

    This is not always true. If the value type is declared within a method, it will go on the stack. If it is a class level variable, it will be on the heap since the object which owns it is allocated on the heap.

    So for MathLibrary, the 2 integers will also be on the heap since they are fields of that object.

    For all reference types, the memory will be allocated in the heap, which will be later flushed out or managed by the GC.

    Yes this is true. However, one important thing to remember here is that the object will be allocated on the heap but if this object is created within a method, the reference for the object will be on the stack. The actual object will be on the heap. For example:

    public void Foo()
    {
       Foo f = new Foo();
    }
    

    The reference "f" will be on the stack, but the actual object the reference is referencing will be on the heap.


    Well your understanding about value types is wrong! Generally there are these two golden rules:

  • A Reference type will be allocated on heap.
  • Value types and pointers (reference to other objects) will be allocated where they are declared, not always on stack!
  • besides, there are these two facts about Stack and Heap :

  • The Stack is responsible for keeping track of what's executing in our code (method calls, passing parameters, ...).
  • Heap is responsible for keeping track of objects.
  • So in your example, both values types (number1 and number2) are declared in a class which is a reference type so the object which is an instance of your class will be allocated on Heap as so will both of the values types! And since maths object is in-fact a reference to the actual object it totally depends where it was declared! For instance if it was declared in a method, Stack would responsible for keeping tracks of what's been called, so the maths reference would reside on stack and point to the actual object on Heap . But if it was declared as an instance member, it would reside on Heap .

    链接地址: http://www.djcxy.com/p/79144.html

    上一篇: 推进用户的良好Linux / Unix书籍是什么?

    下一篇: 内存管理类实例