Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when does static member get memory

Tags:

c#

.net

oop

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?

like image 930
Vaibhav Jain Avatar asked Apr 16 '10 08:04

Vaibhav Jain


2 Answers

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

like image 94
Simeon Avatar answered Nov 12 '22 12:11

Simeon


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.

like image 36
Oliver Friedrich Avatar answered Nov 12 '22 13:11

Oliver Friedrich