-3

I noticed that static fields of value type are stored on the heap without boxing.

For instance/non-static value type fields, I know that they are treated as parts of the object (containing the fields) on the heap so that the reference of that object is copied to the stack frame during relevant method call.

But since static value type fields do not associate with specific instance of the class, may I ask how static value type fields on the heap are accessed and modified in stack frame?

For a simple example:

class TrialsOnB
{
    public static int _b = 0;

    public int AccessB()
    {
        return _b;
    }

    public void ModifyB(int x)
    {
        _b = x;
    }
}

From the accepted answer under the duplicate link, my current understanding is that in C# and Java all (kinds of) static fields are stored inside the Type object of the class. So if a stack frame ever exists and if the data in it ever refer to static field, it may copy the reference of the Type object and do manipulations.

If my understanding is incorrect, could anyone please correct me? Many thanks

J-A-S
  • 368
  • 1
  • 8
  • What exacty do you ask? How the IL code looks like? – Klaus Gütter Jul 24 '21 at 03:54
  • @KlausGütter Thank you for your comment. I mean, is ```_b``` accessed/modified in a stack frame directly by its reference on the heap? Or is it belongs to some object and being accessed/modified in stack frame as the reference of that object is copied into the stack frame? Or something else? – J-A-S Jul 24 '21 at 04:00
  • The stack frame is not involved. As you can see when you declare your methods static (so that the TrialsOnB instance is not on the stack) – Klaus Gütter Jul 24 '21 at 04:04
  • @KlausGütter Could you please explain more on why stack frame is not involved here if I call ```AccessB``` or ```ModifyB```? In my understanding, a stack frame is created once a method/function is called – J-A-S Jul 24 '21 at 04:12
  • May be (or may be optimized away), but this is irrelevant. AccessB just gets the value from the memory location where the one and only instance of the static field is stored and returns it. – Klaus Gütter Jul 24 '21 at 04:26
  • "static value type" is not a thing. – Dai Jul 24 '21 at 04:27
  • @Dai Sorry I mean static field which is of value type. – J-A-S Jul 24 '21 at 04:36

1 Answers1

0

Yes, it can be changed. But, what makes static variable unique is static variables belongs to the class instead of a particular object.

We can create as many objects we need for a class. These objects will have their own copy instance variables. But only one copy of static variable will be available for static variables across all objects. If one objects changes the value of the static variable, it will reflect in other objects too.

We can also access the static variables without creating object for that class. Static variables are the first ones which are loaded into the memory when a class is loaded.

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

Pls see this

Mansur Kurtov
  • 726
  • 1
  • 4
  • 12