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?
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With