Reputation: 34407
I have a class which have a static member. As I understand, all static members are common for all instances of the class, which means static members would be allocated memory only once. Where is this memory allocated (Stack or Heap) and when does this memory get allocated?
EDIT: This memory is different from a instance level memory. How does this memory get referenced? Does this memory get allocated at the time of compilation?
Upvotes: 10
Views: 4562
Reputation: 9240
The memory allocation for static members is done just when the type is used for the time, be it as declaration for a variable or access to a static member.
As already stated, memory allocation for static members is done on the heap.
Upvotes: 1
Reputation: 5579
Static members are always stored in the global heap, even reference type members. However, this heap is not the normal garbage collected heap. Find out more here: http://www.codeproject.com/KB/cs/codeconcepts.aspx
Upvotes: 4
Reputation: 16032
This memory as allocated on heap. Each type has static constructor which performs initialization of the type. It is executed before type is accessed.
Upvotes: 0