Static variables are stored on a CLR (per application domain) private heap,
called the "High Frequency" Heap as in Jon's article.
The static variables do contain the actual values, that means an int
variable is stored as a 32bit entity.
Boxing a static variable is just like boxing an instance variable, the value
is wrapped in an object of that type and stored on the GC heap.
Consider following code :
static int i = 255;
void SomeMethod() {
Object o = i; //boxing operation
...
If we watch the heaps and the stack after the assignment, we'll see this
(the addresses are fictive)
High Freq. Heap
address value
00cd0068 000000FF
|
box
|
V
GC Heap
address value
01282000 00000000 Sync. # Synch. block index
01282004 790ec308 Type Handle pointer to Method Table for
System.Int32 type
01282008 000000FF value
stack (of current thread)
0012f420 01282004 o -----> Reference to object o of type System.Int32
What we see is that the static variable contains the actual value but
doesn't carry any type information.
The boxed value is GC heap allocated object of type System.Int32, pointed to
by a GC tracked reference variable o on the stack.
Willy.
"Mark R. Dawson" <MarkRDawson@discussions.microsoft.com> wrote in message
news:DE62770D-9C34-46FF-8927-0DCBBDC8F675@microsoft.com...[color=blue]
> Hi John,
> that is a really excellent article. So when static value types are
> allocated are they boxed and stored on the heap, or just stored in the
> heap
> as a value?
>
> Thanks
> Mark.
>
> "Jon Skeet [C# MVP]" wrote:
>[color=green]
>> Mark R. Dawson <MarkRDawson@discussions.microsoft.com> wrote:
>>
>> <snip>
>>[color=darkred]
>> > Value types are stored on the stack (one of the
>> > types of areas of memory allocated to a process), the stack also
>> > stores
>> > function parameters when you make a function call and return pointers
>> > to
>> > allow the program to know where it needs to jump back to after a
>> > function
>> > call.[/color]
>>
>> Stacks aren't only stored on the stack, and this description has caused
>> numerous people problems in the past. See
>>
http://www.pobox.com/~skeet/csharp/memory.html
>>
>> --
>> Jon Skeet - <skeet@pobox.com>
>>
http://www.pobox.com/~skeet
>> If replying to the group, please do not mail me too
>>[/color][/color]