Essentially, no memory allocation is required for constants during runtime, but dynamic memory is allocated for fields when the instance where the field is located is created.
There are several issues that need to be discovered in depth,
First, it is precisely because the constant does not have the relevant memory address that the compiler will determine during compilation that all the values on the constant symbol are replaced. Asp.net may cause version Problems Based on this mechanism, for example:
IfProgramThe constant in Set B has changed due to business factors, so assembly B must be re-compiled. However, if other assembly that referenced the assembly B constant previously had to be re-compiled.
Otherwise, the constant value will always use the previous value. This is the version issue.
Second, when the field will be allocated memory,
In the case of instance fields, when a machine instruction named newobj in the intermediate language instruction is compiled by JIT to generate the machine instruction, the CLR will calculate the space required for all fields and
Allocate the corresponding space in the managed heap. Many languages have operators such as new, such as C #, which compile and generate il commands such as newobj.
There is another situation,When the deserialization object and memberwiseclone are used as object copies, the memory is allocated and the members of the object are initialized.CodeThis is because the C ++ code of the kernel is called directly. It may only be a copy of bytes.
For a type field, that is, a static field, I personally think that the type will be initialized before the type is accessed, that is, the caller Type constructor, at this time, the memory will be allocated, which may be before.
Introduction:
Note: Some methods, such as system. objet. memberwiseclone (), are labeled with [methodimplattritions (methodimploptions. internalcall)] In Asp.net. This part of code is invisible using the Il tool.
Internal callend up making a call to a C ++ function in the CLR. You can find them back inRotor source code. Look at CLR \ SRC \ VM \ eCall. cpp to find the mapping from the. NET visible name to the CLR function name. Beware that the source is getting dated.
(For personal opinion, please correct me. Thank you .)