Busy work is always easy for us to ignore the most basic knowledge, the hands of a stop, go downstairs to breathe fresh air (Beijing friends are sorry), let the brain switch the process.
Think back to the difficulties we encountered in our work. Well, many of them are based on the fact that we do not have a thorough understanding of the basic knowledge, or we only know what is on the surface. We are often fooled by these superficial things. The last thing we wait for is the increase in the number of bugs, low performance, unstable operation, sharp increase in maintenance costs, and a decrease in customer satisfaction, more serious, it may lead to a vicious circle of projects. We can imagine how difficult the challenges we are facing.
To put it bluntly, the main content of this section is variable memory allocation in. net.
1. Let's first look at the member variables of the following class:
Public Class Customer
{
Int customerid;
String customername;
}
Public Class Customer
{
Int customerid = 0;
String customername = NULL;
}
At first glance, we will feel that the second method is more reasonable, but our compiler is not so intelligent.He will always first help us assign the member variable an initial value. When we manually assign the initial value, the compiler will replace the default initialization value with the initialization value we assign before calling the default constructor.. That is, unless the initialized data is business-related data, we assign the default value to the JIT increase the burden.
Ii. Static variables:
When we add a customercount int type static variable and assign the initial value 0, we will find that,The compiler creates a static constructor by default.And replace the default value initialized by the compiler in the static constructor. If we do not initialize it manually, the compiler will automatically provide us with a static constructor, unless our class has no static variables.The static constructor is executed only once..
3. constants:
1. We knowNo need to allocate memory for static ConstantsAndThe initialization value must be assigned when it must be defined.This advantage is not difficult to find that he will bring latent bugs. When his reference (actually the constant value) is distributed to differentProgramIf the constant value needs to be changed in the later stage due to business changes, the project must be re-compiled once as a whole; otherwise, other referenced assembly constants still keep the original value. We can see that this makes maintenance inconvenient.
2. Because of the inconvenience caused by static constants, dynamic constants are created at this time,Dynamic Constants need to allocate memoryIt must be declared or assigned an initialization value in the constructor. If no value is assigned manually, the compiler will initialize the default value, but such a constant is meaningless.
Iv. Local variables:
We all know that before accessing a local variable in C #, We must assign an initial value to it. If no value is assigned, the compilation will not pass. In fact, the compiler is very clever at this time. We declare a local variable, but don't assign it a value. What should we do with this local variable (the closure is not in this scope )? It is not a class member variable and can be shared. In turn, let's think about how heavy the JIT burden is if the compiler also assigns initial values to local variables. SoBy default, C # Does not initialize local variables..
Let's take a look at the memory allocation of local variables:
Public Class Customer
{
Public customer getcustomer ()
{
Int customerid = 4;
Customer customer = getcustomerbyid (customerid );
Return customer;
}
Private customer getcustomerbyid (int id)
{
Customer customer = new customer ();
Return customer;
}
}
Okay. Let's take a look at the figure below:
Note: ① assign a value to the local variable customerid.
③ Press this and mermerid values into the stack and call the getcustomerbyid method.
Note: ⑤ call the getcustomerbyid Method
7. consumer creates a customer object on the hosting stack and assigns a value to the customer variable.
Note: When callback assigns a value to the returned returnobj value. returnobj is a temporary storage created by the compiler. The returned value is a local variable.
Worker pushes the returned content to the stack.
Note: After the ⑫ merbyid method is executed, the return value is passed to the stack maintained by the caller. At this time, the getcustomerbyid local variables are popped up by the stack, and the memory is automatically reclaimed.
Dimensions assigns a value to the local variable customer.
⑭ ⑮ Assigns a value to the local variable returnobj.
Worker pushes the return value to the stack.
To illustrate the memory allocation of local variables.CodeMemory usage during execution, JIT will also perform a lot of Optimization for us, and the local variable table is not reflected in the stack.
From this, we can know that the value of the local variable's median type is stored in the stack (Not all value types are stored in the stack zone. exceptions such as closure and yield are special. The compiler will improve the local variable), And the reference type is to open the memory in the managed heap to store the reference type object, and its object reference is stored in the stack.
-- Aaron. Pan