none
static variable stored in heap or stack in c# RRS feed

  • Question

  • static variable stored in heap or stack in c#

    where are static reference type or static value type store in heap or stack memory?

    Wednesday, May 15, 2013 12:28 PM

Answers

  • Static variables are stored on the Managed Heap, not the Stack, when the type is first referenced.  The Type Object of the compiled class contains a reference to the object. 

    The Type Object of a class will stay in memory until the AppDomain where it resides is unloaded.  Since the object on the Heap is always being referenced by the compiled Type Object, static objects on the Heap will never by GC'ed and will always consume memory until the AppDomain is unloaded

    http://msdn.microsoft.com/en-us/magazine/cc163791.aspx#S14

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.com/



    Wednesday, May 15, 2013 1:34 PM
  • Well, just thinking about it logically, it can't be on the stack.  I can't think of a way that it could be on the stack and have it work, can you?  There would always be a way to exit the function that created storage for the static -- making it essentially a local, not static.  So, it can't be the stack.

    * Unless the optimizing compiler somehow knew that there were no references to it beyond local scope and decided not to make treat it as static in the first place ... I suppose that's possible, but only in the case where the variable is not used outside of the function scope.  And I don't think this optimization is made.

    Wednesday, May 15, 2013 1:18 PM

All replies

  • Well, just thinking about it logically, it can't be on the stack.  I can't think of a way that it could be on the stack and have it work, can you?  There would always be a way to exit the function that created storage for the static -- making it essentially a local, not static.  So, it can't be the stack.

    * Unless the optimizing compiler somehow knew that there were no references to it beyond local scope and decided not to make treat it as static in the first place ... I suppose that's possible, but only in the case where the variable is not used outside of the function scope.  And I don't think this optimization is made.

    Wednesday, May 15, 2013 1:18 PM
  • Static variables are stored on the Managed Heap, not the Stack, when the type is first referenced.  The Type Object of the compiled class contains a reference to the object. 

    The Type Object of a class will stay in memory until the AppDomain where it resides is unloaded.  Since the object on the Heap is always being referenced by the compiled Type Object, static objects on the Heap will never by GC'ed and will always consume memory until the AppDomain is unloaded

    http://msdn.microsoft.com/en-us/magazine/cc163791.aspx#S14

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.com/



    Wednesday, May 15, 2013 1:34 PM