views:

156

answers:

3

It is somewhat known where .NET keeps value types in memory (mostly in stack but could be in heap in certain circumstances etc)...

My question is - where is the code of the struct?

If I have say 16 byte of data fields in the struct and a massive computation method in it - I am presuming that 16 byte will be copied in stack and the method code is stored somewhere else and is shared for all instances of the struct.

Are these presumptions correct?

+1  A: 

Yes. Basically methods are managed separately in some structure that - basically - is not SO well known and documented (as noone ever needs it).

it is also kept as bytecode, compiled, may also be inlined in other methods.

The struct is known by type, so the calls to the methods can be routed properly.

TomTom
+4  A: 

The MSIL is stored in the code section of the assembly - which Windows maps into memory when the assembly is first loaded. When the method is first executed the JIT will compile the MSIL to x86/x64 code. Once the method is compiled into memory it usually stays there and will be shared by all threads. There are some circumstances where multiple AppDomains will cause the MSIL to be compiled a second time, but it's rare.

Paul Alexander
+1  A: 

This is a great article for figuring out what goes where.

Mark Dickinson
thank you. I saw this article before but then lost!