Life Course .NET Core CSharp primary articles category

.NET Core CSharp primary articles 1-7

This section is a class lifecycle

introduction

Whether the object is a what? For many beginners, the object is a very abstract knowledge. If I have to describe in one sentence, I think, "Everything Is an Object" is for the most comprehensive overview of the subject. In this section, we will assemble a fruit handset in rich soil Kang Zhang working whole egg as an example, a detailed explanation of all aspects of object-oriented.

Construct an object of class

"Zhang whole egg, you go to the fruit company, to assemble their parts come with a list of demands - and also to bring the assembly of technical specifications." Yelling workshop director is Zhang whole eggs services. Zhang went to the whole egg fruit company, got his wish to get what he wanted, says the assembly parts list:

  • amoled screen * 1
  • Battery 3000MA * 1
  • CPU*1
  • Memory * 1

Technical note reads:

  • Assembly parts: a screen placed on top, at the bottom of the battery, the PCB interposed therebetween, to seal the upper PCB CPU and memory
  • Power method: Long press the power button for three seconds

Due to space limitations, we only list them, you can find our list of the top assembly, in fact, is part of our mobile phones, we need to take up space inside the phone, and is an important parameter of this phone. This and the functionality of our class properties and fields are the same; and technical notes for this specific operation, they are a process, an action, not an entity, and therefore they are a function of our class is one meaning.

Suddenly an old employee said Zhang whole egg, in fact, ah, each with a fruit phones are almost not much difference, you can preset the memory size and the type of CPU in the machine, so you can directly do the mold . Faced with this situation, Zhang whole egg came up with a wonderful, and that is passed in parameter in the constructor.

So we can construct such a class

class FruitPhone
{
    public FruitPhone(int msize,string cpuType)
    {
        CpuType = cpuType;
        MemSize = msize;
    }

    public string ScreenType{get;set}
    public string CpuType{get;set;} 
    public int MemSize{get;set;}
    public int Battery{get;set;}

    void Make()
    {
        //todo
    }
    void Open()
    {
        //todo
    }
}

Born object

Individual objects like people, are born to the WTO, the death of death. Our story from the life of an object begin. First, look at the example above, how an object is born.

FruitPhone p = new FruitPhone(2,"A12");

We call the constructor, successfully created a target phone, while the phone is created, although we have not assembled a kind of screen, but we also need to reserve their space in the phone dies, so the object instances of the time, each field will be initialized its interior.

Screen for a cell type and, according to our follow-up costs, etc. may be adjusted to
birth as is only necessary to complete the initialization of the field, other data is accomplished through subsequent operations. Attribute assignment for example, to obtain the necessary information like.

Object creation in memory

About allocate memory, you should first understand the problem of where to allocate. CLR manages memory area, there are three, namely:

  • A thread's stack, for example assign a value type. Stack mainly by the operating system, while the garbage collector is not controlled, when the end value type instance where the method storage unit is automatically released. High efficiency of the stack, but the limited storage capacity.
  • GC heap, for dispensing a small object instance. If the reference instance of an object type size is less than 85,000 bytes, in the instance it will be assigned GC heap, memory allocation, or when there is recovered, the garbage collector GC heap might be compressed, as detailed in the text about.
  • LOH (Large Object Heap) stack for dispensing large object instance. If the reference type object instance size is not less than 85,000 bytes, the instance will be assigned to the heap LOH, and LOH stack is not compressed, and only when fully recovered is recovered GC.

Allocated on the stack for local variables, the operating system maintains a stack pointer to the address of the next free space, and the stack memory address is filled from the higher to lower down.

For instance reference type assigned to the managed heap, and the thread stack is the place to start object life cycle. After 32-bit processors, application initialization process is completed, the CLR allocated a reserved address space on the available address space of the process, it is the processes (each available 4GB) of memory on an available address space region, but it does not correspond to any physical memory, this address space that is managed heap. Depending managed heap and divided into a plurality of information storage areas, in which the most important is the garbage collected heap (Heap GC) and loaded stack (Loader Heap), GC Heap for storing object instances, managing by GC; Loader Heap divided for the High-Frequency heap, Low-Frequency heap and Stub heap, different heap and store different
information. Loader Heap most important information is the information related to the metadata, i.e. Type objects, each embodied as a Type Method Table (method table) on Loader Heap, while Method Table metadata recorded in the stored information, e.g. yl type, static fields, implement interfaces, and so on all the way. Loader Heap from GC control, its life cycle from creation to unload AppDomain.

For this embodiment the object is created, a pointer to the heap is first declaration in the data stack pointer (reference), which occupies 4 bytes, and then call newobj instruction, searching whether the class containing the parent class, if any, from parent starts to allocate memory for this embodiment, FruitPhone objects required 4-byte string memory references to two 4-byte int * 2. The total number of bytes occupied by the object instance of the total number of bytes plus an additional member of the desired object, and wherein the additional member comprises TypeHandle SyncBlockIndex, a total of eight bytes (32-bit CPU platform), a total of 24 bytes.

CLR AppDomain current corresponding managed heap search to find a 20-byte contiguous unused space, and assign the memory address. In fact, the GC using very efficient algorithm to satisfy the request, only to promote NextObjPtr pointer forward 20 bytes, and clears the original NextObjPtr pointer byte pointer and the current between the NextObjPtr,
then return to the original pointer address NextObjPtr i.e. available, the address is the address of the newly created managed heap object, which is the address pointed to examples cited p. At a time NextObjPtr still point to the next location of the newly created object. Divided attention, the stack is to
expand the low address, and heap allocation is extended to higher addresses.

Finally, call the object constructor, object-initialization, to complete the process. The construction process can be subdivided into
the following links:

  • FruitPhone Type object type configuration, comprising a main static field, the method table, implemented interface, and
    assign the managed heap mentioned above Loader Heap.
  • Two additional members of the initialization of p: TypeHandle and SyncBlockIndex. The TypeHandl
    E MethodTable pointer to the Loader Heap, CLR will be located according to a specific Type TypeHandle;
    the SyncBlockIndex Synchronization Block pointer to the memory block, for example in a multi-threaded environment
    synchronization object.
  • FruitPhone constructor call, initialize instance fields. Initialize instance, will first upwardly recursively perform
    row in the parent class initialization, until type System.Object initialization, then execution returns to the initialization subclass until
    execution FruitPhone class so far. In the present embodiment, the initialization process is performed first System.Object class, direct execution FruitPhone. Finally, the memory address managed heap allocated newobj, thi FruitPhone is transmitted to the
    s-parameters, p and references declared on the stack to pass.

The above-described process is basically completed creating a type, memory allocation, and the entire process of initializing a reference, but the process must be regarded as a simplified description, the actual implementation process is more complicated, involving a series of operations and process refinement.

(Insert image memory)

supplement

Memory allocation and deallocation of static fields, what is different?

Static fields are also stored in the method table, located on the rear of the slot array method table, its life cycle from creation to AppDomain is
unloaded. Therefore, a type no matter how many objects are created, its static fields in memory only one. Static field only by the static
Tense constructor initializes, to ensure that before the static constructor to create any type of objects, or any static field or method
before performing referenced, a detailed discussion refer to the execution sequence.

The demise of objects

In this section, we first observe the object of death, in order to reflect on and appreciate the philosophy of human accession to the WTO, the comparison of the two will give us more inspiration on their own. Lifecycle object by GC control, the rule something like this: GC manage all the managed heap object when garbage collection execution, GC inspection object on the managed heap is no longer used, and perform memory reclamation operations. Objects are not used by the application, it refers to the object without any reference. On how to recycle, recovery time, as well as recyclable object traversing algorithm, is a more complex issue, we will discuss in-depth follow-up. However, this recovery process, we make the same emotion. Nature is the invisible GC, the Creator of all things but eventually recovered, can not be changed. We can do that will broaden the cycle of life, prolong, writing a more exciting

Reference

You must know .NET

Github

BiliBili Home

WarrenRyan's Blog

Blog Park

Guess you like

Origin www.cnblogs.com/WarrenRyan/p/11256571.html