none
Memory allocation between a class variable and local variable? RRS feed

  • Question

  • Hi,

    As the title says how does memory allocation work on class variable and local variable? I know that class variable will allocate memory as soon as the variable is declared. Will local variable allocate memory during run time? Local variable will be destroyed as soon as the method ends, right? 🤔

    Wednesday, May 29, 2019 12:04 PM

All replies

  • Hello,

    There is no see answer, see the following in depth article.

    Memory in .NET - what goes where. In regards to memory releasing see GC class.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, May 29, 2019 12:32 PM
    Moderator
  • The article Karen referenced is packed with a lot of good information, even though it is short.  It is worthy of study.  There are a couple of things worth emphasizing.

    C# has the concept of "value types" and "references types", and they have different memory strategies.  Consider:

        static public void func()
        {
            int i = 4;
            int[] t = new int[] { 1,2,3,4 };
        }

    "i" has a value type.  The variable i is stored on the stack, and gets magically freed when the stack is popped at the end of the function.  This works just like it does in C.

    "t" has a reference type.  The variable t is stored on the stack, BUT what's stored there is just a pointer.  The actual array, which is what you think of as "t", is stored as an anonymous object on the heap.  The variable "t" just points to that anonymous object.  When the function exits, the stack space for the pointer is released, but the object lives on.  In this particular case, after "t" goes away, there will no longer be any references to that anonymous object, so the garbage collector will eventually delete it.

    A member variable in a class is created when an instance of that class is created.  If the class is a reference type, it will always live in the heap, and the memory is released when the object itself is deleted.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Wednesday, May 29, 2019 6:02 PM
  • Hi,

    If you want to know more about memory allocation in c#, you could look at the following link.

    https://www.codeproject.com/Articles/1058126/Memory-allocation-in-Net-Value-type-Reference-type

    Hope my link could be helpful.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, May 30, 2019 1:32 AM
    Moderator
  • Actually for primitive variable types such as i here, most likely no memory will be allocated as the compiler may choose to use register to store it.

    https://msdn.microsoft.com/en-us/magazine/dn973015.aspx

    [quote]

    When targeting the CLR, the compiler has to emit Common Intermediate Language (CIL) code that models a stack machine. In this case, the compiler won’t perform register allocation (though if some of the emitted code is native, register allocation will be performed on it, of course) and will postpone it until runtime to be performed by the just-in-time (JIT) compiler (or the Visual C++ back end in the case of .NET Native compilation). RyuJIT, the JIT compiler that ships with the .NET Framework 4.5.1 and later, implements a pretty decent register allocation algorithm.

    [/quote]

    https://blogs.msdn.microsoft.com/ericlippert/2012/01/16/what-is-the-defining-characteristic-of-a-local-variable/


    Thursday, May 30, 2019 3:04 AM
    Answerer