D
D
Denis Bredun2020-07-17 18:21:53
C++ / C#
Denis Bredun, 2020-07-17 18:21:53

Where is the static class in memory?

In what memory is a static class, on the heap, in theory? A static class is a type object that contains a table of methods, but only static and static constructor?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
ayazer, 2020-07-17
@Luffy1

, but method tables are created for each class object individually.

no, you are confusing something. 1 table for each type, it makes no sense to create a copy for each object. well, now the answer to the rest of the question becomes obvious - there is no difference between the method table for a static / non-static class - everything lies somewhere in the heap in the AppDomain.
upd:
In what memory is a static class, on the heap, in theory?
of course, only not in the one that is SOH / LOH, but in the one that Richter mentions as "in fact, there are more than 2 heaps, but you don't need the rest." And to be more precise - in high frequency heap. I don't know why you need this knowledge, but now live with it.
Статический класс - это объект-тип, который содержит таблицу методов, но только статических и статического конструктора?

meh, actually so-so wording. better stop on the fact that in the context of c # - a static object is a special case of an object that can only contain static fields / methods.

M
MrDywar Pichugin, 2020-07-18
@Dywar

I remembered that I read about this in the book "Optimization of Applications on the .Net Platform".


Method table
The method table pointer field refers to an internal CLR structure called method table, which in turn refers to another internal structure called EEClass (where EE is short for Execution Engine). Together, the method table and EEClass contain the information needed to select a virtual method, an interface method, a static variable, determine the type of an object at run time, access base class methods, and many other purposes. The method table contains frequently used information required to perform operations by mechanisms such as the virtual method selection mechanism, while the EEClass structure contains information that is used less frequently, such as by the reflection mechanism. You can get acquainted with the contents of both data structures using the commands !DumpMT and !
An array of references to static fields is fixed so that its address cannot be changed by the garbage collector, in addition, static fields of simple types are placed inside the method table, which is not affected by the garbage collector. This ensures that hardwired addresses can be safely used to access such fields.

There are tables where all the information is stored.
See windbg for more details.
Class example:
class MyClass
{
    private string _field1 = "Some string 1";
    public string Field2 { get; set; }
}

0:003> !do 0000005400006600
Name:        ConsoleApplication1.MyClass
MethodTable: 00007ffa2b5c4378
EEClass:     00007ffa2b6d2548
Size:        32(0x20) bytes
File:        E:\...\ConsoleApplication1.exe
Fields:
              MT    Field   Offset                 Type VT     Attr            Value Name
00007ffa89d60e08  4000002        8        System.String  0 instance 0000005400006620 _field1
00007ffa89d60e08  4000003       10        System.String  0 instance 00000054000035a0 <Field2>k__BackingField

If interested, put SOS and play.
I already did this, and posted the results in https://habr.com/ru/post/260047/
They downvoted, I didn’t know much then. But that's history.

P
Peter, 2020-07-17
@petermzg

What "Method Table" are you talking about?
If you are talking about VMT, then from the name virtual method table it should be clear that this is only for virtual methods, which are not in C# static classes.
So from a static class, except for its metadata, you do not need to store anything.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question